8

I have a multi tenant application where the administrators can add new customers from the GUI. This will set up a customer specific site where the url would be something like : customerName.mydomain.com. At the moment I then have to go into IIS to add that URL to the bindings for my site. How can I do this from the C# code?

IIS version is 7 or higher.

From answer below I ended up with the following:

You have to give write access to the folder "C:\Windows\System32\inetsrv\config\" to the user the Site is running under

var server = new ServerManager();
var site = server.Sites.FirstOrDefault(a => a.Name.Contains("mydomain"));
if (site != null)
{
    site.Bindings.Add($"*:80:{customer}.mydomain.com", "http");
    server.CommitChanges();
}
devzero
  • 2,510
  • 4
  • 35
  • 55
  • which IIs version.. also check answer here http://stackoverflow.com/questions/1286831/programatically-create-a-web-site-in-iis-using-c-sharp-and-set-port-number – lordkain Aug 16 '16 at 08:53

1 Answers1

4

For IIS 7 and above there is an API https://blogs.msdn.microsoft.com/carlosag/2006/04/17/microsoft-web-administration-in-iis-7/

From the link:

ServerManager iisManager = new ServerManager();
iisManager.Sites[“NewSite”].Applications.Add(“/Sales”, “d:\\MyApp”);
iisManager.Update(); 

That should get you on the way

ste-fu
  • 6,879
  • 3
  • 27
  • 46