sadier

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
Create a Web Site in IIS Using C#Ever wondered how to interface with IIS using .NET? If all your apps/clients are running IIS 6.0, it is drop dead easy, just enable the XML metabase and parse it to your heart's content. However, if you need to support older clients, you will probably end up using DirectoryServices. However, since DirectoryServices is fairly generic, you might have difficulty figuring out what exactly you need to do. In case you get stuck, here is a little reference block of code that will create a new web site with the supplied name and root directory:

public class SetupUtility{public SetupUtility(){}public int CreateWebSite(string webSiteName, string pathToRoot){return CreateWebSite(webSiteName, pathToRoot, false);}public int CreateWebSite(string webSiteName, string pathToRoot, bool createDir){DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");// Find unused ID value for new web siteint siteID = 1;foreach(DirectoryEntry e in root.Children){if(e.SchemaClassName == "IIsWebServer"){int ID = Convert.ToInt32(e.Name);if(ID >= siteID){siteID = ID+1;}}}// Create web siteDirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);site.Invoke("Put", "ServerComment", webSiteName);site.Invoke("Put", "KeyType", "IIsWebServer");site.Invoke("Put", "ServerBindings", ":80:");site.Invoke("Put", "ServerState", 2);site.Invoke("Put", "FrontPageWeb", 1);site.Invoke("Put", "DefaultDoc", "Default.aspx");site.Invoke("Put", "SecureBindings", ":443:");site.Invoke("Put", "ServerAutoStart", 1);site.Invoke("Put", "ServerSize", 1);site.Invoke("SetInfo");// Create application virtual directoryDirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");siteVDir.Properties["AppIsolated"][0] = 2;siteVDir.Properties["Path"][0] = pathToRoot;siteVDir.Properties["AccessFlags"][0] = 513;siteVDir.Properties["FrontPageWeb"][0] = 1;siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/"+siteID+"/Root";siteVDir.Properties["AppFriendlyName"][0] = "Root";siteVDir.CommitChanges();site.CommitChanges();return siteID;}}
posted on Wednesday, September 17, 2003 4:06 AM 
 
Comments 
# re: Create a Web Site in IIS Using C# 
Jonathan Cogley
I wonder if this is how the NAnt mkiisdir task does it? 
http://nantcontrib.sourceforge.net/help/tasks/mkiisdirtask.html 

Hmmm, it looks like it uses the DirectoryServices approach that you mention. 
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/nantcontrib/NAntContrib/src/Tasks/IISTasks.cs?rev=1.4&content-type=text/vnd.viewcvs-markup
Posted @ 9/17/2003 6:54 AM  
# re: Create a Web Site in IIS Using C# 
Matt Miller
Great sample! 
I used it as the basis for a program I wrote recently, and I was getting an error message saying application unavailable whenever I tried to connect to the web site. 

As soon as I removed this line; 
siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/"+siteID+"/Root"; the site was created correctly and I no longer got that error. All of the web sites being created by my program are asp.net sites so I am uncertain whether this occurs in all web sites or only asp.net sites. 

I checked the AppRoot property for several sites created manually in IIS and in each case the AppRoot property either didn't exist or was set to "".
Posted @ 10/26/2003 9:58 PM  
# re: Create a Web Site in IIS Using C# 
Mike
Jesse you are a legend, I have been trying to figure this one out for ages. Thanks. Mike
Posted @ 12/28/2003 6:57 AM  
# re: Create a Web Site in IIS Using C# 
ASP scripts will not run in IIS 6.0
Just in case anyone has any problems getting asp scripts after using the above code. 

Simply replace this line: 
siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/"+siteID+"/Root"; 

with this line: 
siteVDir.Properties["AppRoot"][0] = "/LM/W3SVC/"+siteID+"/Root"; 

that one "/" makes all the difference. :) 

-Matt Brooks
Posted @ 1/16/2004 9:01 PM  
# re: Create a Web Site in IIS Using C# 
Panayot Belchev
Thank you great reference. It is almost impossible to find examples on the internet on this topic. I implemented this in VB.NET as well, and added some more functionality like Bandwidth and LogFile setup. 

I have uploaded the code here: 

http://www.panayot.com/articles/iis.htm 

It is tested on IIS 6.0 

By the way it is a little dangerous to edit the MetaBase.xml file directly because if a malformated xml is saved to this file, this could crash the IIS server and force it to revert to an older backup of the metabase file if such exists. 

Panayot
Posted @ 1/26/2004 9:30 PM  
# re: Create a Web Site in IIS Using C# 
Panayot Belchev
I will add explanation and a link for the VB.NET code to be downloaded in a while.
Posted @ 1/26/2004 9:49 PM  
# re: Create a Web Site in IIS Using C# 
Loreal
thanks,but how about using in IIS5.0? 
soon i'll try.
Posted @ 3/31/2004 3:15 AM  
# re: Create a Web Site in IIS Using C# 
Jesse Ezell
This works perfectly fine in IIS 5.0.
Posted @ 3/31/2004 4:14 AM  
# re: Create a Web Site in IIS Using C# 
William Bartholomew
Something to be aware of in IIS 6.0 is that the siteIDs are no longer sequential, they are random, so just adding 1 to the siteID does not necessarily mean you will get a unique siteID. A better approach would be generating a random number (as the IIS Manager appears to), checking if it exists, and if not using it, if so repeat.
Posted @ 4/14/2004 7:29 AM  
# re: Create a Web Site in IIS Using C# 
Found wrong!!!
Compiler Error Message: CS0234: The type or namespace name 'DirectoryServices' does not exist in the class or namespace 'System' (are you missing an assembly reference?) 

what's the question?
Posted @ 4/21/2004 1:47 PM  
# re: Create a Web Site in IIS Using C# 
wil
You need to add DirectoryServices.dll as a reference
Posted @ 4/21/2004 9:28 PM  
# re: Create a Web Site in IIS Using C# 
wil
I'm getting Access denied on when running the code. Are there special permissions I need to set? 

I copied the code verbatim, and called it with 
SetupUtility su = new SetupUtility (); 

su.CreateWebSite("test", "c:\inetpub\www\test", true); 

Exception details System.Runtime.InteropServices.COMException: Access is denied 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Posted @ 4/21/2004 9:36 PM  
# re: Create a Web Site in IIS Using C# 
david
sexy
Posted @ 4/27/2004 7:36 PM  
# re: Create a Web Site in IIS Using C# 
Bart Fibrich
Thanks for this.. Helped me a lot. 

If you want the website to be attached to the DefaultApplicationPool you need to do something like. 

DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir"); 
siteVDir.Properties["Path"][0] = pathToRoot; 
siteVDir.Properties["AccessScript"][0] = true; 
siteVDir.CommitChanges(); 
siteVDir.Properties["AppFriendlyName"][0] = "Default Application"; 
siteVDir.Invoke("AppCreate3", new object[] {2, "DefaultAppPool", true}); 
siteVDir.CommitChanges(); 

Not sure why it needs the double CommitChanges but its what I had to do to get it working for the ROOT 
virtual directory. Works without the double commit for others. Strange!
Posted @ 5/4/2004 4:53 AM  
# re: Create a Web Site in IIS Using C# 
eric
Same problem as will 

site.Invoke("SetInfo"); 

causes an access denied. 



IIS is using the default IUSR_ComputerName user for anonymous access. This user is a member of guests account so windows will not allow this account to touch active directory. 
You can do two things: 
1. open the website (or virtual directory) properties in IIS Manager, go to security, "Authentication and access control", click edit and change the default user to a more powerful user like Administrator. This means of course that whatever code is executed from this website, will have admin privileges, so be careful. 

2. the other alternative is to impersonate your code with another user and then revert back to the default iis user. I have put an example here: http://www.panayot.com/articles/access-denied-iis.aspx






posted on 2004-10-15 14:32  毛小华  阅读(1067)  评论(0)    收藏  举报