Previous Next

Thread: Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)

Last post 12-04-2007 11:53 AM by mshabib. 6 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (7 items)

Sort Posts:

  • 11-27-2007, 6:21 PM

    • mshabib
    • Top 500 Contributor
    • Joined on 11-27-2007, 12:37 PM
    • Posts 14

    Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)

    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 starts here:

     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(@"%SystemRoot%
    \Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"), "-i");// + "/
    W3SVC/" + random_site_id.ToString() + "/Root");
                        virtual_directory.Close();
                        site.Close();

    RunApplication(Environment.ExpandEnvironmentVariables(@"%SystemRoot%
    \Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"), "-s " + "/
    W3SVC/" + random_site_id.ToString() +"/Root");

    RunApplication(Environment.ExpandEnvironmentVariables(@"%SystemRoot%
    \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;
                }

  • 12-02-2007, 3:01 AM In reply to

    • thomad
    • Top 25 Contributor
    • Joined on 08-20-2002, 3:28 PM
    • Redmond
    • Posts 387

    Re: Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)

    Mshabib,

    The CreateNewWebSite call would do the whole trick for you. You wouldn't have to create a random site number and you wouldn't have to create a root vdir. Here is how you would call it:

    string serverComment = "test";
    string path = "c:\\inetpub\\test";
    string serverBindings = ":80:test";
    DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
    object[] newSite = new object[]{serverComment, new object[]{serverBindings}, path};
    object siteId = (object)w3svc.Invoke("CreateNewSite", newSite);

    Then you can still modify the settings on the virtual directory.

    Hope this helps.

    Thomas Deml
    Senior Program Manager
    Internet Information Services
    Microsoft Corp.
  • 12-02-2007, 2:33 PM In reply to

    • mshabib
    • Top 500 Contributor
    • Joined on 11-27-2007, 12:37 PM
    • Posts 14

    Re: Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)

    Thanks for the info -- I was hoping I could use that method when I first started looking into programmatically creating a website, but according to this:

     

    http://msdn2.microsoft.com/en-us/library/ms525336.aspx

     

    It is only available on the .NET framework v3.0 (or above), and I'm on .NET  v2.0. Thanks for the tip, though.

     

    -Mustafa 

  • 12-02-2007, 8:20 PM In reply to

    • thomad
    • Top 25 Contributor
    • Joined on 08-20-2002, 3:28 PM
    • Redmond
    • Posts 387

    Re: Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)

    Mustafa,

     It is safe to use this method. It has nothing to do with the .NET Framework version. Sometimes the .NET Framework guys don't know what they are talking about :)

     

    Thomas Deml
    Senior Program Manager
    Internet Information Services
    Microsoft Corp.
  • 12-03-2007, 7:37 PM In reply to

    • mshabib
    • Top 500 Contributor
    • Joined on 11-27-2007, 12:37 PM
    • Posts 14

    Re: Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)

    thanks.

     

    i fixed my original problem for anyone who wants to know. on the line that said:

     

    virtual_directory.Properties["AppRoot"][0] = "LM/W3SVC/" + random_site_id.ToString() +"/Root"; 

     I had to change it to

    virtual_directory.Properties["AppRoot"][0] = "/LM/W3SVC/" + random_site_id.ToString() +"/Root";
     

  • 12-04-2007, 11:40 AM In reply to

    • thomad
    • Top 25 Contributor
    • Joined on 08-20-2002, 3:28 PM
    • Redmond
    • Posts 387

    Re: Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)

    Mshabib,

    I don't see a difference between the two lines.

    Thomas Deml
    Senior Program Manager
    Internet Information Services
    Microsoft Corp.
  • 12-04-2007, 11:53 AM In reply to

    • mshabib
    • Top 500 Contributor
    • Joined on 11-27-2007, 12:37 PM
    • Posts 14

    Re: Creating website via DirectoryServices in C# but will not serve aspx pages (htm ok)

     there's a slash at the beginning of the second line.

Page 1 of 1 (7 items)
Page view counter