« Previous Next »

Not Answered Thread: How do you set the 'Default Documents' property using Microsoft.Web.Administration.dll

Last post 11-27-2006 11:44 PM by CarlosAg. 3 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (4 items)

Sort Posts:

  • 09-14-2006, 4:30 PM

    • dhacker
    • Top 200 Contributor
    • Joined on 09-05-2006, 5:48 PM
    • Posts 33

    How do you set the 'Default Documents' property using Microsoft.Web.Administration.dll

    Is there a way to set the 'Default Documents' property using Microsoft.Web.Administration.dll?  I used to do that by setting the MD_DEFAULT_LOAD_FILE property but I would like to code it using the new IIS 7functionality.  I would prefer not do this but editing an xml file or using the inetmgr.exe.  An example would be really appreciated:)
  • 09-14-2006, 5:46 PM In reply to

    • OscarGS
    • Not Ranked
    • Joined on 04-21-2005, 5:14 PM
    • Posts 6

    Re: How do you set the 'Default Documents' property using Microsoft.Web.Administration.dll

    Do you want to edit the collection entries for the default document section?

    cheers,

    OscarGS

  • 09-14-2006, 6:14 PM In reply to

    • OscarGS
    • Not Ranked
    • Joined on 04-21-2005, 5:14 PM
    • Posts 6

    Re: How do you set the 'Default Documents' property using Microsoft.Web.Administration.dll

    Here is a quick sample on how to list the collection entries of the defaultDocument section file collection.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Web.Administration;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ServerManager serverManager = new ServerManager();
                Configuration appHostConfig = serverManager.GetApplicationHostConfiguration();
                ConfigurationSection defaultDocumentSection = appHostConfig.GetSection("system.webServer/defaultDocument");
                foreach (ConfigurationElement element in defaultDocumentSection.GetCollection("files"))
                {
                    Console.WriteLine(element.GetAttribute("value").Value);
                }
               
            }
        }
    }

  • 11-27-2006, 11:44 PM In reply to

    Re: How do you set the 'Default Documents' property using Microsoft.Web.Administration.dll

    Here is one way you can do it. This will update the web.config file at the Default Web Site and set the default document to just be default.aspx. You can also use the GetWebConfiguration method for an Application if you only want to set it for a specific one (mgr.Sites[0].Application["/MyApp"].GetWebConfiguration()).

        ServerManager mgr = new ServerManager();
        
    Configuration config 
            
    mgr.Sites["Default Web Site"].GetWebConfiguration();

        
    ConfigurationElementCollection documents =
            
    config.GetSection("system.webServer/defaultDocument").GetCollection("files");

        
    documents.Clear();
        
    ConfigurationElement document documents.CreateElement();
        
    document["value""default.aspx";
        
    documents.Add(document);

        
    mgr.CommitChanges();
Page 1 of 1 (4 items)
Microsoft Communities