我想设置一个管理页面(ASP.NET/C#),它可以将IIS主机标题添加到托管管理页面的网站。这可能吗?以编程方式将IIS主机头添加到网站
我不想添加一个http头 - 我想模仿手动进入IIS的动作,调出网站的属性,点击网站选项卡上的高级选项,以及高级网站标识屏幕和新的带有主机头值,IP地址和TCP端口的“身份”。
我想设置一个管理页面(ASP.NET/C#),它可以将IIS主机标题添加到托管管理页面的网站。这可能吗?以编程方式将IIS主机头添加到网站
我不想添加一个http头 - 我想模仿手动进入IIS的动作,调出网站的属性,点击网站选项卡上的高级选项,以及高级网站标识屏幕和新的带有主机头值,IP地址和TCP端口的“身份”。
这里有Adding Another Identity To A Site Programmatically RSS
论坛此外,这里有一个关于如何Append a host header by code in IIS的文章:
下面的示例添加一个主机头在IIS网站。这涉及到更改ServerBindings属性。没有Append方法可用于将新的服务器绑定附加到此属性,因此需要完成的操作是读取整个属性,然后再将其与新数据一起添加回来。这是在下面的代码中完成的。 ServerBindings属性的数据类型为MULTISZ,字符串格式为IP:端口:主机名。
请注意,此示例代码不会执行任何错误检查。每个ServerBindings条目都是唯一的,而且你 - 程序员 - 负责检查它(这意味着你需要遍历所有条目并检查是否要添加什么是唯一的)是非常重要的。
using System.DirectoryServices;
using System;
public class IISAdmin
{
/// <summary>
/// Adds a host header value to a specified website. WARNING: NO ERROR CHECKING IS PERFORMED IN THIS EXAMPLE.
/// YOU ARE RESPONSIBLE FOR THAT EVERY ENTRY IS UNIQUE
/// </summary>
/// <param name="hostHeader">The host header. Must be in the form IP:Port:Hostname </param>
/// <param name="websiteID">The ID of the website the host header should be added to </param>
public static void AddHostHeader(string hostHeader, string websiteID)
{
DirectoryEntry site = new DirectoryEntry("IIS://localhost/w3svc/" + websiteID);
try
{
//Get everything currently in the serverbindings propery.
PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
//Add the new binding
serverBindings.Add(hostHeader);
//Create an object array and copy the content to this array
Object [] newList = new Object[serverBindings.Count];
serverBindings.CopyTo(newList, 0);
//Write to metabase
site.Properties["ServerBindings"].Value = newList;
//Commit the changes
site.CommitChanges();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
public class TestApp
{
public static void Main(string[] args)
{
IISAdmin.AddHostHeader(":80:test.com", "1");
}
}
但我不知道如何遍历头值做错误检查提及。
我想知道如何做到这一点,使用户能够在我的系统中指向自己的域名。 – Ben 2009-01-20 09:21:10