How can I either create a new website or add a host header to an existing IIS 7 server from code?
I have looked and have not been able to find a working example?
How can I either create a new website or add a host header to an existing IIS 7 server from code?
I have looked and have not been able to find a working example?
One solution would be to create a custom HttpModule that does the work for you, however it does require you to be have a DNS that supports wildcards (*). If your DNS does not support that, you might look into managing your own DNS.
That said, here's a good post on creating an HttpModule that parses the "subdomain" passed in and forwards the traffic to the appropriate spot. He's using a search mechanism [to locate content with keywords matching the subdomain], but it can be modified for you own needs.
http://codebetter.com/blogs/brendan.tompkins/archive/2006/06/27/146875.aspx
[EDIT] Another solution would be to find a DNS provider that offers a programmable DNS service, perhaps through a web service. You would then programatically add a subdomain to that DNS when needed from your application. That's a super simplified explanation, and doesn't take into account your business needs. Personally I prefer the HttpModule option for adding subdomains within an application as it requires less modification of the server(s) involved.
What version of .Net are you using?
If you are using .Net 3.0 or 3.5, and if you only need to configure IIS7 (not 6 or 5), check out the Microsoft.Web.Administration namespace -- it should have everything you need.
If you are using an older version of .net, have a look at WMI.
I don't have any WMI code for IIS 7 (we have a setup for an intranet application, but it uses IIS 6-compatible WMI). But, here's a link to a tool you can use for figuring out the WMI stuff: http://www.microsoft.com/downloads/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en It will actually generate c# (or vb.net) code for manipulating WMI. For IIS 7, I think that the root WMI namespace is root\WebAdministration.
Also, have a look at this link, it might help Get to Know the IIS 7.0 WMI Provider Using CIM Studio