« Previous Next »

Thread: Adding a handler to web.config via c#

Last post 07-17-2009 5:53 AM by crazystick. 5 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (6 items)

Sort Posts:

  • 07-15-2009, 10:53 AM

    Adding a handler to web.config via c#

    Hi All,

     I have an issue with adding a handler to a web.config file using c# (its being executed as part of a custom installer action). I am trying the code below, but I'm getting an error "The configuration section 'system.webServer/handlers' cannot be read because it is missing a section declaration"

     If I copy all the <configSections> stuff from applicationHost then it succeeds, but is this really the only way to get this working? I would have expected that using the WebConfigurationMap object and specifying the default machine.config would give me enough.

     I have other handlers already defined in this web.config, and they work fine without additional configSection definitions.

    public static void AddHandlerToWebConfig(string webConfigPath, string name, string path, string type)

    {

    using (ServerManager serverManager = new ServerManager())

    {

    // get specified web.config file

    Configuration webConfig = serverManager.GetWebConfiguration(new WebConfigurationMap(new WebConfigurationMap().MachineConfigurationPath,webConfigPath),null);

    ConfigurationSection handlersSection = webConfig.GetSection("system.webServer/handlers");

    ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();

    // add the specified handler

    ConfigurationElement addElement = handlersCollection.CreateElement("add");

    addElement["name"] = name;

    addElement["path"] = path;

    addElement["type"] = type;

    addElement["verb"] = "*";addElement["preCondition"] = "integratedMode,runtimeVersionv2.0";

    handlersCollection.Add(addElement);

    // save

    serverManager.CommitChanges();

    }

    }

     Thanks, Paul

     Update: you can't redefine the system.webServer/handlers section in the web.config as its already defined in applicationHost, so that workaround doesn't work anyway.

  • 07-15-2009, 3:42 PM In reply to

    • anilr
    • Top 10 Contributor
    • Joined on 05-23-2006, 10:13 PM
    • Redmond, WA
    • Posts 2,343

    Re: Adding a handler to web.config via c#

    You seem to be using the wrong overload for GetWebConfiguration - don't you want to use this one instead?

    Anil Ruia
    Senior Software Design Engineer
    IIS Core Server
  • 07-15-2009, 5:20 PM In reply to

    Re: Adding a handler to web.config via c#

    As Anil mentioned you need to call a different overload, if you want to add it in a specific sites web.config you can call:

    Configuration webConfig = serverManager.GetWebConfiguration("SiteNameGoesHere");

    Or if you want inside an application or a folder inside a site, you can:

    Configuration webConfig = serverManager.GetWebConfiguration("SiteNameGoesHere", "/YourApp");

     

     

  • 07-16-2009, 5:58 AM In reply to

    Re: Adding a handler to web.config via c#

    Thanks for the answers. The reason I was using that overload is because this code is running in a custom action. During the install, the user selects the installation directory where the web application is already installed, and so I don't know the site name or the virtual path, only the path to the web.config file.

     Does this mean that there's no way to modify a given web.config file through these APIs?

  • 07-16-2009, 8:15 PM In reply to

    Re: Adding a handler to web.config via c#

    The design of the API's are to modify Web.config files using the Site name which will resolve the physical path by using the system.applicationHost/sites section in ApplicationHost.config.

    For that reason it is not possible to modify in this way an arbitrary file, especially if the intent is to add a section that is IIS specific since the hierarchy "MACHINE/WEBROOT/APPHOST" means that we cannot map WebROOT to do that, you need to do it post-apphost so that the section definitions are found. So for that reason, it is not possible.

    Having said that, is it not possible for your MSI to request the Site name to the user? Or maybe lookup the physical path in the sites section and find the Site name and use it?

  • 07-17-2009, 5:53 AM In reply to

    Re: Adding a handler to web.config via c#

    Thanks, that makes sense. You are right, I can do more work in the msi to get the site from the user, just wanted to know if there was an easier way.

     Thanks for your help.

Page 1 of 1 (6 items)
Microsoft Communities