c# - 将主机头添加到 IIS 网站的代码

标签 c# iis-7 active-directory

我有一个有很多名字的网站。我希望能够以编程方式向 IIS 添加一个新的主机头记录,以允许它识别另一个名称。具体来说,将新的主机头添加到给定站点的代码是什么(最好是在 C# 中)?

最佳答案

static void Main(string[] args)
{
    AddHostHeader(1, "127.0.0.1", 8080, "fred");
    AddHostHeader(1, null, 8081, null);
}

static void AddHostHeader(int? websiteID, string ipAddress, int? port, string hostname)
{
    using (var directoryEntry = new DirectoryEntry("IIS://localhost/w3svc/" + websiteID.ToString()))
    {
        var bindings = directoryEntry.Properties["ServerBindings"];
        var header = string.Format("{0}:{1}:{2}", ipAddress, port, hostname);

        if (bindings.Contains(header))
            throw new InvalidOperationException("Host Header already exists!");

        bindings.Add(header);
        directoryEntry.CommitChanges();
    }
}

关于c# - 将主机头添加到 IIS 网站的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/784005/

相关文章:

c# - 为什么这种碰撞检测方法没有击中所有物体?

c# - IIS7重启问题

c# - 检查所有鼠标按钮的按钮状态

iis-7 - IIS URL 重写 - 友好 URL : Querystring variable is repeated

c# - 在iis中启用svg解压

C# Active Directory 组查询

c# - 从 Active Directory 组获取用户

powershell - 在 Powershell 中将变量作为开关参数传递

c# - 弹性查询以从mongodb数据C#中消除空格

c# - C# "formatting"标准是什么?