Creating website via DirectoryServices in C# but will not serve aspx
Creating website via DirectoryServices in C# but will not serve aspx
am 28.11.2007 00:26:49 von MMAS
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.
code as follows:
public static int CreateNewWebsite(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_site_id = false;
int random_site_id = 1;
do
{
bool regenerate_site_id = false;
System.Random random_generator = new Random();
random_site_id = random_generator.Next();
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int current_site_id =
Convert.ToInt32(e.Name);
if (current_site_id == random_site_id)
{
regenerate_site_id = true;
break;
}
}
}
found_valid_site_id = !regenerate_site_id;
} while (!found_valid_site_id);
// Create web site
DirectoryEntry site =
(DirectoryEntry)root.Invoke("Create", "IIsWebServer", random_site_id);
site.Invoke("Put", "ServerComment", website_name +
String.Format(" - ({0})", port));
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerBindings", ":" +
port.ToString() + ":");
site.Invoke("Put", "ServerState", 2);
//site.Invoke("Put", "FrontPageWeb", 1);
//site.Invoke("Put", "DefaultDoc",
"Default.aspx,Default.html,Default.html,Index.aspx,Index.htm ,Index.html");
// site.Invoke("Put", "SecureBindings", ":443:");
site.Invoke("Put", "ServerAutoStart", 1);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");
//create app website directory
site.AuthenticationType =
AuthenticationTypes.Anonymous;
// site.Username = username;
// site.Password = password;
// DirectoryEntry website_directory =
site.Children.Add("Root", "IIsWebDirectory");
// website_directory.Properties["Location"][0] = "/
LM/W3SVC/" + random_site_id.ToString() + "/root/aspnet_client";
// website_directory.Properties["AccessFlags"][0] =
"AccessRead";
// website_directory.Properties["DirBrowseFlags"]
[0] = "0";
// website_directory.CommitChanges();
// Create application virtual directory
DirectoryEntry virtual_directory =
site.Children.Add("Root", "IISWebVirtualDir");
virtual_directory.Properties["AppIsolated"][0] =
2;
if (path_to_website_root.EndsWith("\\"))
{
path_to_website_root =
path_to_website_root.Substring(0, path_to_website_root.Length - 1);
}
virtual_directory.Properties["Path"][0] =
path_to_website_root;
virtual_directory.Invoke("AppCreate", true);
virtual_directory.Properties["EnableDirBrowsing"]
[0] = false ;
virtual_directory.Properties["AccessExecute"][0] =
true;
virtual_directory.Properties["AccessRead"][0] =
true;
virtual_directory.Properties["AccessWrite"][0] =
false;
virtual_directory.Properties["AuthAnonymous"][0] =
true;
virtual_directory.Properties["AuthBasic"][0] =
false;
virtual_directory.Properties["AuthNTLM"][0] =
true;
virtual_directory.Properties["AppFriendlyName"][0]
= website_name;
virtual_directory.Properties["AppRoot"][0] = "LM/
W3SVC/" + random_site_id.ToString() +"/Root";
virtual_directory.CommitChanges();
site.CommitChanges();
//
RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"), "-i");// + "/
W3SVC/" + random_site_id.ToString() + "/Root");
virtual_directory.Close();
site.Close();
RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"), "-s " + "/
W3SVC/" + random_site_id.ToString() +"/Root");
RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.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.Diagnostics.ProcessStartInfo psi = new
System.Diagnostics.ProcessStartInfo(location);
psi.Arguments = arguments;
psi.RedirectStandardOutput = true;
psi.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput =
listFiles.StandardOutput;
listFiles.WaitForExit(2000);
string output = "";
if (listFiles.HasExited)
{
output = myOutput.ReadToEnd();
}
return output;
}
Re: Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)
am 28.11.2007 01:25:13 von Ken Schaefer
What do you mean by "it will not serve ASPX pages"?
Cheers
Ken
--
My IIS Blog: www.adOpenStatic.com/cs/blogs/ken
"MMAS" wrote in message
news:efbdd12d-3a54-4fda-a637-56b079f74686@n20g2000hsh.google groups.com...
> 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.
>
> code as follows:
>
> public static int CreateNewWebsite(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_site_id = false;
> int random_site_id = 1;
> do
> {
> bool regenerate_site_id = false;
> System.Random random_generator = new Random();
> random_site_id = random_generator.Next();
>
> foreach (DirectoryEntry e in root.Children)
> {
> if (e.SchemaClassName == "IIsWebServer")
> {
>
> int current_site_id =
> Convert.ToInt32(e.Name);
> if (current_site_id == random_site_id)
> {
> regenerate_site_id = true;
> break;
> }
> }
> }
>
> found_valid_site_id = !regenerate_site_id;
> } while (!found_valid_site_id);
>
> // Create web site
>
> DirectoryEntry site =
> (DirectoryEntry)root.Invoke("Create", "IIsWebServer", random_site_id);
>
> site.Invoke("Put", "ServerComment", website_name +
> String.Format(" - ({0})", port));
>
> site.Invoke("Put", "KeyType", "IIsWebServer");
>
> site.Invoke("Put", "ServerBindings", ":" +
> port.ToString() + ":");
>
> site.Invoke("Put", "ServerState", 2);
>
> //site.Invoke("Put", "FrontPageWeb", 1);
>
> //site.Invoke("Put", "DefaultDoc",
> "Default.aspx,Default.html,Default.html,Index.aspx,Index.htm ,Index.html");
>
> // site.Invoke("Put", "SecureBindings", ":443:");
>
> site.Invoke("Put", "ServerAutoStart", 1);
>
> site.Invoke("Put", "ServerSize", 1);
>
> site.Invoke("SetInfo");
>
> //create app website directory
> site.AuthenticationType =
> AuthenticationTypes.Anonymous;
> // site.Username = username;
> // site.Password = password;
>
> // DirectoryEntry website_directory =
> site.Children.Add("Root", "IIsWebDirectory");
> // website_directory.Properties["Location"][0] = "/
> LM/W3SVC/" + random_site_id.ToString() + "/root/aspnet_client";
> // website_directory.Properties["AccessFlags"][0] =
> "AccessRead";
> // website_directory.Properties["DirBrowseFlags"]
> [0] = "0";
> // website_directory.CommitChanges();
>
> // Create application virtual directory
> DirectoryEntry virtual_directory =
> site.Children.Add("Root", "IISWebVirtualDir");
>
> virtual_directory.Properties["AppIsolated"][0] =
> 2;
>
> if (path_to_website_root.EndsWith("\\"))
> {
> path_to_website_root =
> path_to_website_root.Substring(0, path_to_website_root.Length - 1);
> }
>
> virtual_directory.Properties["Path"][0] =
> path_to_website_root;
> virtual_directory.Invoke("AppCreate", true);
>
> virtual_directory.Properties["EnableDirBrowsing"]
> [0] = false ;
> virtual_directory.Properties["AccessExecute"][0] =
> true;
> virtual_directory.Properties["AccessRead"][0] =
> true;
> virtual_directory.Properties["AccessWrite"][0] =
> false;
> virtual_directory.Properties["AuthAnonymous"][0] =
> true;
> virtual_directory.Properties["AuthBasic"][0] =
> false;
> virtual_directory.Properties["AuthNTLM"][0] =
> true;
> virtual_directory.Properties["AppFriendlyName"][0]
> = website_name;
> virtual_directory.Properties["AppRoot"][0] = "LM/
> W3SVC/" + random_site_id.ToString() +"/Root";
>
> virtual_directory.CommitChanges();
>
> site.CommitChanges();
> //
> RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
> \Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"), "-i");// + "/
> W3SVC/" + random_site_id.ToString() + "/Root");
> virtual_directory.Close();
> site.Close();
>
> RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
> \Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"), "-s " + "/
> W3SVC/" + random_site_id.ToString() +"/Root");
>
> RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
> \Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.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.Diagnostics.ProcessStartInfo psi = new
> System.Diagnostics.ProcessStartInfo(location);
> psi.Arguments = arguments;
> psi.RedirectStandardOutput = true;
> psi.WindowStyle =
> System.Diagnostics.ProcessWindowStyle.Hidden;
> psi.UseShellExecute = false;
> System.Diagnostics.Process listFiles;
> listFiles = System.Diagnostics.Process.Start(psi);
> System.IO.StreamReader myOutput =
> listFiles.StandardOutput;
> listFiles.WaitForExit(2000);
> string output = "";
> if (listFiles.HasExited)
> {
> output = myOutput.ReadToEnd();
>
> }
>
> return output;
> }
Re: Creating website via DirectoryServices in C# but will not serve
am 01.12.2007 00:16:33 von MMAS
On Nov 27, 6:25 pm, "Ken Schaefer"
wrote:
> What do you mean by "it will not serve ASPX pages"?
>
> Cheers
> Ken
>
> --
> My IIS Blog:www.adOpenStatic.com/cs/blogs/ken
>
> "MMAS" wrote in message
>
> news:efbdd12d-3a54-4fda-a637-56b079f74686@n20g2000hsh.google groups.com...
>
> > 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.
>
> > code as follows:
>
> > public static int CreateNewWebsite(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_site_id = false;
> > int random_site_id = 1;
> > do
> > {
> > bool regenerate_site_id = false;
> > System.Random random_generator = new Random();
> > random_site_id = random_generator.Next();
>
> > foreach (DirectoryEntry e in root.Children)
> > {
> > if (e.SchemaClassName == "IIsWebServer")
> > {
>
> > int current_site_id =
> > Convert.ToInt32(e.Name);
> > if (current_site_id == random_site_id)
> > {
> > regenerate_site_id = true;
> > break;
> > }
> > }
> > }
>
> > found_valid_site_id = !regenerate_site_id;
> > } while (!found_valid_site_id);
>
> > // Create web site
>
> > DirectoryEntry site =
> > (DirectoryEntry)root.Invoke("Create", "IIsWebServer", random_site_id);
>
> > site.Invoke("Put", "ServerComment", website_name +
> > String.Format(" - ({0})", port));
>
> > site.Invoke("Put", "KeyType", "IIsWebServer");
>
> > site.Invoke("Put", "ServerBindings", ":" +
> > port.ToString() + ":");
>
> > site.Invoke("Put", "ServerState", 2);
>
> > //site.Invoke("Put", "FrontPageWeb", 1);
>
> > //site.Invoke("Put", "DefaultDoc",
> > "Default.aspx,Default.html,Default.html,Index.aspx,Index.htm ,Index.html");
>
> > // site.Invoke("Put", "SecureBindings", ":443:");
>
> > site.Invoke("Put", "ServerAutoStart", 1);
>
> > site.Invoke("Put", "ServerSize", 1);
>
> > site.Invoke("SetInfo");
>
> > //create app website directory
> > site.AuthenticationType =
> > AuthenticationTypes.Anonymous;
> > // site.Username = username;
> > // site.Password = password;
>
> > // DirectoryEntry website_directory =
> > site.Children.Add("Root", "IIsWebDirectory");
> > // website_directory.Properties["Location"][0] = "/
> > LM/W3SVC/" + random_site_id.ToString() + "/root/aspnet_client";
> > // website_directory.Properties["AccessFlags"][0] =
> > "AccessRead";
> > // website_directory.Properties["DirBrowseFlags"]
> > [0] = "0";
> > // website_directory.CommitChanges();
>
> > // Create application virtual directory
> > DirectoryEntry virtual_directory =
> > site.Children.Add("Root", "IISWebVirtualDir");
>
> > virtual_directory.Properties["AppIsolated"][0] =
> > 2;
>
> > if (path_to_website_root.EndsWith("\\"))
> > {
> > path_to_website_root =
> > path_to_website_root.Substring(0, path_to_website_root.Length - 1);
> > }
>
> > virtual_directory.Properties["Path"][0] =
> > path_to_website_root;
> > virtual_directory.Invoke("AppCreate", true);
>
> > virtual_directory.Properties["EnableDirBrowsing"]
> > [0] = false ;
> > virtual_directory.Properties["AccessExecute"][0] =
> > true;
> > virtual_directory.Properties["AccessRead"][0] =
> > true;
> > virtual_directory.Properties["AccessWrite"][0] =
> > false;
> > virtual_directory.Properties["AuthAnonymous"][0] =
> > true;
> > virtual_directory.Properties["AuthBasic"][0] =
> > false;
> > virtual_directory.Properties["AuthNTLM"][0] =
> > true;
> > virtual_directory.Properties["AppFriendlyName"][0]
> > = website_name;
> > virtual_directory.Properties["AppRoot"][0] = "LM/
> > W3SVC/" + random_site_id.ToString() +"/Root";
>
> > virtual_directory.CommitChanges();
>
> > site.CommitChanges();
> > //
> > RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
> > \Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"), "-i");// + "/
> > W3SVC/" + random_site_id.ToString() + "/Root");
> > virtual_directory.Close();
> > site.Close();
>
> > RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
> > \Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"), "-s " + "/
> > W3SVC/" + random_site_id.ToString() +"/Root");
>
> > RunApplication(Environment.ExpandEnvironmentVariables(@"%Sys temRoot%
> > \Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.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.Diagnostics.ProcessStartInfo psi = new
> > System.Diagnostics.ProcessStartInfo(location);
> > psi.Arguments = arguments;
> > psi.RedirectStandardOutput = true;
> > psi.WindowStyle =
> > System.Diagnostics.ProcessWindowStyle.Hidden;
> > psi.UseShellExecute = false;
> > System.Diagnostics.Process listFiles;
> > listFiles = System.Diagnostics.Process.Start(psi);
> > System.IO.StreamReader myOutput =
> > listFiles.StandardOutput;
> > listFiles.WaitForExit(2000);
> > string output = "";
> > if (listFiles.HasExited)
> > {
> > output = myOutput.ReadToEnd();
>
> > }
>
> > return output;
> > }
The website will serve normal html pages but not aspx pages, as in:
the .net interpreter doesn't work.