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 fileConfiguration webConfig = serverManager.GetWebConfiguration(new WebConfigurationMap(new WebConfigurationMap().MachineConfigurationPath,webConfigPath),null);
ConfigurationSection handlersSection = webConfig.GetSection("system.webServer/handlers");ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();
// add the specified handlerConfigurationElement 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.