Asp.net|iis saw a drunk solution thousand sorrow of the ASP.net version of the IIS directory, think of the previous thought to the code to IIS to increase the host head, but has not to study, today, while interest, decided to solve the problem.
for the blog site, if you need to provide users with level two domain support, and the Web program is not running the default site, you need to register the user through the code to the IIS to add the appropriate host header.
This issue is through Google search to append a host header by the code in IIS resolved, after testing, confirm the method is feasible and some improvements to the code, considering this content will help some friends, so wrote this article.
The
code is as follows:
static void Main (string[] args)
{
addhostheader (1, NULL, N, "test.cnblogs.com");
}
static void addhostheader (int siteid,string IP, int port, string domain) br> {
DirectoryEntry site = new DirectoryEntry ("iis://localhost/w3svc/" +siteid);
PropertyValueCollection serverbindings = Site. properties["ServerBindings"];
String headerstr = string. Format ("{0}:{1}:{2}", Ip,port,domain);
if (!serverbindings.contains (HEADERSTR))
{
Serverbindings.add (HEADERSTR);
}
site.commitchanges ();
}
before finding append a host header by code in IIS, I took some detours by not finding the "ServerBindings" attribute in the following code.
DirectoryEntry site = new DirectoryEntry ("Iis://localhost/w3svc/1/root");
The code is very simple, need to explain is SiteID, the default site is 1, for non-default sites, by viewing the site log file name can be known.