IIS Helper, binding new domain using c#
public class IISHelper
{
public static string SiteName
{
get
{
return Config.GetSetting("SiteName");
}
}
public static void AddDomianTmSuite(string portalAlias)
{
using (ServerManager iis = new ServerManager())
{
Site site = iis.Sites[SiteName];
if (site != null)
{
Binding binding = GetBinding(site.Bindings, "*:80:" + portalAlias);
if (binding == null)
{
AddBinding(site.Bindings, "*:80:" + portalAlias);
}
iis.CommitChanges();
}
}
}
public static void DeleteDomainTmSuite(string portalAlias)
{
using (ServerManager iis = new ServerManager())
{
Site site = iis.Sites[SiteName];
if (site != null)
{
Binding binding = GetBinding(site.Bindings, "*:80:" + portalAlias);
if (binding != null)
{
site.Bindings.Remove(binding);
}
iis.CommitChanges();
}
}
}
/// <summary>
/// Get Bingding
/// </summary>
/// <param name="bindings">collection of currnet site bindings</param>
/// <param name="BindingInformation">like *:80:asp.google.tst</param>
/// <returns>null or binding</returns>
public static Binding GetBinding(BindingCollection bindings, string BindingInformation)
{
foreach (Binding binding in bindings)
{
if (binding.BindingInformation.ToLower() == BindingInformation.ToLower())
{
return binding;
}
}
return null;
}
/// <summary>
/// Add Binding
/// </summary>
/// <param name="bindings">collection of currnet site bindings</param>
/// <param name="BindingInformation">like *:80:asp.google.tst</param>
public static void AddBinding(BindingCollection bindings, string BindingInformation)
{
Binding binding2 = bindings.CreateElement();
binding2.BindingInformation = BindingInformation;
binding2.Protocol = @"http";
bindings.Add(binding2);
}
}
官方参考:
http://www.iis.net/ConfigReference/system.applicationHost/sites/site/bindings