On Nov 26, 5:44 pm, MMAS <mustafasha...@ gmail.comwrote:
I've seen a similar post about this problem but still cannot find an
automated solution. Below is the code I'm using to create a website
(for IIS 6.0) via C#. once the site is created, it will not serve aspx
pages. If I:
1. go to IIS Manager
2. view the properties for the site in question
3. go to the [Home Directory] tab
4. click [Remove] next to the Application Name textbox in the
Application Settings area.
5. click on [Add] to add the application back in.
The site works as expected.
Can anyone help me out? Feel free to email me at
mustafashabib [at] sbcglobal [dot] net
thanks.
sorry, the code is as follows:
public static int CreateNewWebsit e(string website_name, int port,
string path_to_website _root)
{
try
{
DirectoryEntry root = new DirectoryEntry( "IIS://
localhost/W3SVC");
// Find unused ID value for new web site
bool found_valid_sit e_id = false;
int random_site_id = 1;
do
{
bool regenerate_site _id = false;
System.Random random_generato r = new Random();
random_site_id = random_generato r.Next();
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassN ame == "IIsWebServ er")
{
int current_site_id =
Convert.ToInt32 (e.Name);
if (current_site_i d == random_site_id)
{
regenerate_site _id = true;
break;
}
}
}
found_valid_sit e_id = !regenerate_sit e_id;
} while (!found_valid_s ite_id);
// Create web site
DirectoryEntry site =
(DirectoryEntry )root.Invoke("C reate", "IIsWebServ er", random_site_id) ;
site.Invoke("Pu t", "ServerComment" , website_name +
String.Format(" - ({0})", port));
site.Invoke("Pu t", "KeyType", "IIsWebServer") ;
site.Invoke("Pu t", "ServerBindings ", ":" +
port.ToString() + ":");
site.Invoke("Pu t", "ServerStat e", 2);
//site.Invoke("Pu t", "FrontPageW eb", 1);
//site.Invoke("Pu t", "DefaultDoc ",
"Default.aspx,D efault.html,Def ault.html,Index .aspx,Index.htm ,Index.html");
// site.Invoke("Pu t", "SecureBindings ", ":443:");
site.Invoke("Pu t", "ServerAutoStar t", 1);
site.Invoke("Pu t", "ServerSize ", 1);
site.Invoke("Se tInfo");
//create app website directory
site.Authentica tionType =
AuthenticationT ypes.Anonymous;
// site.Username = username;
// site.Password = password;
// DirectoryEntry website_directo ry =
site.Children.A dd("Root", "IIsWebDirector y");
// website_directo ry.Properties["Location"][0] = "/
LM/W3SVC/" + random_site_id. ToString() + "/root/aspnet_client";
// website_directo ry.Properties["AccessFlag s"][0] =
"AccessRead ";
// website_directo ry.Properties["DirBrowseFlags "]
[0] = "0";
// website_directo ry.CommitChange s();
// Create application virtual directory
DirectoryEntry virtual_directo ry =
site.Children.A dd("Root", "IISWebVirtualD ir");
virtual_directo ry.Properties["AppIsolate d"][0] =
2;
if (path_to_websit e_root.EndsWith ("\\"))
{
path_to_website _root =
path_to_website _root.Substring (0, path_to_website _root.Length - 1);
}
virtual_directo ry.Properties["Path"][0] =
path_to_website _root;
virtual_directo ry.Invoke("AppC reate", true);
virtual_directo ry.Properties["EnableDirBrows ing"]
[0] = false ;
virtual_directo ry.Properties["AccessExec ute"][0] =
true;
virtual_directo ry.Properties["AccessRead "][0] =
true;
virtual_directo ry.Properties["AccessWrit e"][0] =
false;
virtual_directo ry.Properties["AuthAnonym ous"][0] =
true;
virtual_directo ry.Properties["AuthBasic"][0] =
false;
virtual_directo ry.Properties["AuthNTLM"][0] =
true;
virtual_directo ry.Properties["AppFriendlyNam e"][0]
= website_name;
virtual_directo ry.Properties["AppRoot"][0] = "LM/
W3SVC/" + random_site_id. ToString() +"/Root";
virtual_directo ry.CommitChange s();
site.CommitChan ges();
//
RunApplication( Environment.Exp andEnvironmentV ariables(@"%Sys temRoot%
\Microsoft.NET\ Framework\v2.0. 50727\aspnet_re giis.exe"), "-i");// + "/
W3SVC/" + random_site_id. ToString() + "/Root");
virtual_directo ry.Close();
site.Close();
RunApplication( Environment.Exp andEnvironmentV ariables(@"%Sys temRoot%
\Microsoft.NET\ Framework\v2.0. 50727\aspnet_re giis.exe"), "-s " + "/
W3SVC/" + random_site_id. ToString() +"/Root");
RunApplication( Environment.Exp andEnvironmentV ariables(@"%Sys temRoot%
\Microsoft.NET\ Framework\v2.0. 50727\aspnet_re giis.exe"), "-c");
return random_site_id;
}
catch (Exception ex)
{
throw new Exception("An error occurred trying to
create a website.", ex);
}
}
private static string RunApplication( string location,
string arguments)
{
System.Diagnost ics.ProcessStar tInfo psi = new
System.Diagnost ics.ProcessStar tInfo(location) ;
psi.Arguments = arguments;
psi.RedirectSta ndardOutput = true;
psi.WindowStyle =
System.Diagnost ics.ProcessWind owStyle.Hidden;
psi.UseShellExe cute = false;
System.Diagnost ics.Process listFiles;
listFiles = System.Diagnost ics.Process.Sta rt(psi);
System.IO.Strea mReader myOutput =
listFiles.Stand ardOutput;
listFiles.WaitF orExit(2000);
string output = "";
if (listFiles.HasE xited)
{
output = myOutput.ReadTo End();
}
return output;
}