Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to modify custom sections of the web.config file, from a generic .NET application?
    primarykey
    data
    text
    <p>I have a WinForm derived application (note NOT an ASP.NET web application) from where I need to modify a custom section of an arbitrary web.config file. As an example, if my web.config is something like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;configuration&gt; &lt;!-- General web.config stuff follows --&gt; &lt;system.web&gt; &lt;httpRuntime executionTimeout="110" maxRequestLength="1024" requestValidationMode="2.0" /&gt; &lt;/system.web&gt; &lt;MyConfigSection&gt; &lt;GeneralParameters&gt; &lt;param key="Var1" value="value1" /&gt; &lt;/GeneralParameters&gt; &lt;/MyConfigSection&gt; &lt;/configuration&gt; </code></pre> <p>I can easily modify some default parameter, say, for <code>maxRequestLength</code> I'd do this and it would work:</p> <pre><code>//Path to the web.config file string strWebConfigFile = @"C:\My files\web.config"; //Convert absolute path to virtual var configFile = new FileInfo(strWebConfigFile); var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name); var wcfm = new WebConfigurationFileMap(); wcfm.VirtualDirectories.Add("/", vdm); //Open web.config file System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/"); if (config != null) { System.Configuration.ConfigurationSection system_web = config.GetSection("system.web/httpRuntime"); PropertyInformation pi = system_web.ElementInformation.Properties["maxRequestLength"]; pi.Value = 1234; //Set new value //Save config.Save(ConfigurationSaveMode.Modified); } </code></pre> <p>The issue is when I try to modify my custom section. Say, if I wanted to rewrite <code>Var1</code> parameter's value with <code>value2</code>, the following:</p> <pre><code>System.Configuration.ConfigurationSection genParams = config.GetSection("MyConfigSection/GeneralParameters"); </code></pre> <p>returns <code>null</code> and if I call it with just <code>MyConfigSection</code>, it gives me this exception:</p> <blockquote> <p>An error occurred creating the configuration section handler for MyConfigSection: Could not load type 'MyWebApp.Configuration' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=N'.</p> </blockquote> <p>What shall I do here to add that "configuration section handler"?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload