我想设置一个管理页面(ASP.NET / C#),可以将IIS主机标题添加到托管页面所在的网站。这可能吗?

我不想添加http标头 - 我想模仿手动进入IIS的操作,显示网站的属性,点击网站标签上的高级,以及高级网站识别屏幕和新的“同一性QUOT;带有主机头值,ip地址和tcp端口。

有帮助吗?

解决方案

以下是以编程方式为网站添加其他身份RSS 的论坛

另外,这里有一篇关于如何通过IIS中的代码附加主机头

  

以下示例将主机标头添加到IIS中的网站。这涉及更改ServerBindings属性。没有可用于将新服务器绑定附加到此属性的Append方法,因此需要做的是读取整个属性,然后将其与新数据一起再次添加回来。这是在下面的代码中完成的。 ServerBindings属性的数据类型为MULTISZ,字符串格式为IP:Port:Hostname。

     

请注意,此示例代码不执行任何错误检查。重要的是每个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");
    }
}

但我不知道如何循环使用标题值来执行上述错误检查。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top