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

  • Question

  • User-1202834629 posted
    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:)
    Thursday, September 14, 2006 4:30 PM

All replies

  • User-1832218776 posted

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

    cheers,

    OscarGS

    Thursday, September 14, 2006 5:46 PM
  • User-1832218776 posted

    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);
                }
               
            }
        }
    }

    Thursday, September 14, 2006 6:14 PM
  • User-47214744 posted

    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();
    Monday, November 27, 2006 11:44 PM