private void AddResponseHeaders() { if (!this.SiteExists()) { Log.LogError(string.Format(CultureInfo.CurrentCulture, "The website: {0} was not found on: {1}", this.Name, this.MachineName)); return; } Configuration config = this.iisServerManager.GetWebConfiguration(this.Name); ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol"); ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders"); foreach (ITaskItem header in this.HttpResponseHeaders) { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Adding HttpResponseHeader: {0} to: {1} on: {2}", header.ItemSpec, this.Name, this.MachineName)); ConfigurationElement addElement = customHeadersCollection.CreateElement("add"); addElement["name"] = header.ItemSpec; addElement["value"] = header.GetMetadata("Value"); bool headerExists = customHeadersCollection.Any(obj => obj.Attributes["name"].Value.ToString() == header.ItemSpec); if (!headerExists) { customHeadersCollection.Add(addElement); this.iisServerManager.CommitChanges(); } } }
private void AddMimeType() { if (!this.SiteExists()) { Log.LogError(string.Format(CultureInfo.CurrentCulture, "The website: {0} was not found on: {1}", this.Name, this.MachineName)); return; } Configuration config = this.iisServerManager.GetWebConfiguration(this.Name); foreach (ITaskItem mimetype in this.MimeTypes) { ConfigurationSection staticContentSection = config.GetSection("system.webServer/staticContent"); ConfigurationElementCollection staticContentCollection = staticContentSection.GetCollection(); ConfigurationElement mimeMapElement = staticContentCollection.CreateElement("mimeMap"); mimeMapElement["fileExtension"] = mimetype.ItemSpec; mimeMapElement["mimeType"] = mimetype.GetMetadata("Value"); this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Adding MimeType: {0} to: {1} on: {2}", mimetype.ItemSpec, this.Name, this.MachineName)); bool typeExists = staticContentCollection.Any(obj => obj.Attributes["fileExtension"].Value.ToString() == mimetype.ItemSpec); if (!typeExists) { staticContentCollection.Add(mimeMapElement); this.iisServerManager.CommitChanges(); } } }
/// <summary> /// 在站点上添加默认文档名 /// </summary> /// <param name="siteName">站点名</param> /// <param name="defaultDocName">默认文档</param> public static void AddDefaultDocument(string siteName, string defaultDocName) { using (var mgr = new ServerManager(ApplicationHostConfigurationPath)) { Configuration cfg = mgr.GetWebConfiguration(siteName); ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument"); ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files"); ConfigurationElementCollection filesCollection = filesElement.GetCollection(); if (filesCollection.Any(elt => elt.Attributes["value"].Value.ToString() == defaultDocName)) { return; } try { ConfigurationElement docElement = filesCollection.CreateElement(); docElement.SetAttributeValue("value", defaultDocName); filesCollection.Add(docElement); } catch (Exception) { // ignored //this will fail if existing } mgr.CommitChanges(); } }
private void AddElement(ConfigurationElementCollection appSettings, string key, string value) { if (appSettings.Any(t => t.GetAttributeValue("key").ToString() == key)) { appSettings.Remove(appSettings.First(t => t.GetAttributeValue("key").ToString() == key)); } var addElement = appSettings.CreateElement("add"); addElement["key"] = key; addElement["value"] = value; appSettings.Add(addElement); }
private static void EnsureDefaultDocument(IIS.ServerManager iis) { Configuration applicationHostConfiguration = iis.GetApplicationHostConfiguration(); ConfigurationSection defaultDocumentSection = applicationHostConfiguration.GetSection("system.webServer/defaultDocument"); ConfigurationElementCollection filesCollection = defaultDocumentSection.GetCollection("files"); if (!filesCollection.Any(ConfigurationElementContainsHostingStart)) { ConfigurationElement addElement = filesCollection.CreateElement("add"); addElement["value"] = HostingStartHtml; filesCollection.Add(addElement); iis.CommitChanges(); } }