In many situation required to multiple domain in one web application. A specially for web based content management system.
So in this kind of application this is very much important to bind each url with IIS binding, so below are the code is help for the IIS binding by c# code by dynamically, so when new sub domain or domain add then code automatically add binding entry in IIS.
Below are the class for the same…
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using Microsoft.Web.Administration; namespace HostEntry { /// <summary> /// Summary description for Class1 /// </summary> public class Biding { public Biding() { } public Biding(string binding) { this.bindingvalue = binding; } public Biding(string binding, string virtualdirectory) { this.bindingvalue = binding; this.virtualdirectory = virtualdirectory; } private string _virtualdirectory; public string virtualdirectory { get { if(string.IsNullOrEmpty(_virtualdirectory)) return System.Configuration.ConfigurationManager.AppSettings["virtualDirectory"].ToString(); else return _virtualdirectory; } set { _virtualdirectory = value; } } public string bindingvalue { get; set; } public void AddBinding() { if(string.IsNullOrEmpty(bindingvalue)) return; using (ServerManager serverManager = new ServerManager()) { Configuration config = serverManager.GetApplicationHostConfiguration(); ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites"); ConfigurationElementCollection sitesCollection = sitesSection.GetCollection(); ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", this.virtualdirectory); if (siteElement == null) throw new InvalidOperationException("Element not found!"); ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings"); if (bindingsCollection.Where(q => q.Attributes["bindingInformation"].Value.ToString() == "*:80:" + this.bindingvalue).Count() == 0) { ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding"); bindingElement["protocol"] = @"http"; bindingElement["bindingInformation"] = @"*:80:" + this.bindingvalue; bindingsCollection.Add(bindingElement); ////ConfigurationElement bindingElement1 = bindingsCollection.CreateElement("binding"); ////bindingElement1["protocol"] = @"https"; ////bindingElement1["bindingInformation"] = @"*:443:"; ////bindingsCollection.Add(bindingElement1); serverManager.CommitChanges(); } } } public bool BidningExist(ConfigurationElementCollection bindingsCollection) { bool isExist = false; foreach (ConfigurationElement item in bindingsCollection) { if (item != null && item.Attributes["bindingInformation"] != null && item.Attributes["bindingInformation"].Value.ToString() == "*:80:" + this.bindingvalue) { isExist = true; break; } } return isExist; } private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues) { foreach (ConfigurationElement element in collection) { if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase)) { bool matches = true; for (int i = 0; i < keyValues.Length; i += 2) { object o = element.GetAttributeValue(keyValues[i]); string value = null; if (o != null) { value = o.ToString(); } if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase)) { matches = false; break; } } if (matches) { return element; } } } return null; } } }
To use this class we have to add below reference in the project.
Microsoft.Web.Administration;
Below are the code you can use for add binding.
Biding oBiding = new Biding("<<Site Name>>","<<Virtual directory name>>"); oBiding.AddBinding();
Also make sure your application running user have proper access about the process, because this is generally update below sensitive file, and generally normal asp.net or iss user dont have permission to update this file so either you have to give perimission with this file or run the app with administrator account,
I suggest this kind of application we should run on out the asp.net process mean either console or window services.
Let me know if you need any more details about this.
Thanks,
Amit Patel
“Happy Programming”
