Asp. NET adds host headers to IIS via code

Source: Internet
Author: User
Tags header iis domain
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.







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.