简体   繁体   中英

how to create a website with IIS7 and C# NET and other settings

We have some setup scripts that create websites when our game servers are installed. I have a different type of game that I need to do this for, so I am tinkering with C# NET to get the job done. I don't know too much about IIS so I am hoping for help.

This is what I have code wise so far. What I need to do is have it create the website so it is accessible via %ip%/%ip%-%port% (of course these will be replaced by the IP/Port) and have that point to %gameserverroot%httpredirect.

Also, is there any way to verify that Directory Browsing is turned on via C#, and turn it on if it isn't?

Any help I can would be much appreciated. A lot of the IIS7 with C# NET examples are pretty confusing. I have found some information on Stackoverflow, but not enough to complete this.

Thanks!

NOTE: I am answering just the directory browse question as you didn't paste any code (that I could see) to show what you currently have. This example may help you, and if not, please post your full code and I can try to expand my example


Use System.Web.Administration namespace (IIS 7), something like this should work:

(I'm assuming your webSite name is "TestSite", but its really whatever you call it. Just set that to the site name you create)

using(ServerManager serverManager = new ServerManager()) {
   Configuration config = serverManager.GetWebConfiguration("TestSite");

   ConfigurationSection directoryBrowseSection =  
       config.GetSection("system.webServer/directoryBrowse");


   if(directoryBrowseSection["enabled"] != true)
   {
      directoryBrowseSection["enabled"] = true;
   }

   serverManager.CommitChanges();
}

the If(!enabled){} check/loop isn't required, just showing to answer your exact question. You can set to enabled = true without the check for simplicity


Anytime you run into confusion surrounding IIS 7, you should consult the iis7 site. http://www.iis.net/

It is one of the best (imo) product reference guides out there.

Remember in IIS7 everything is controlled by a series of XML files, so everything else is simply changing these. One way to approach programmatic development is to figure out how you'd do it in a web.config and then consult the IIS.net site's config reference for that section. The section pages almost always contains both the XML version AND the C# approach for modification. The above code is mostly from the IIS page on the directoryBrowse configuration section, for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM