简体   繁体   中英

Edit httpErrors section in web.config from code behind

Is it possible to edit the "httpErrors" in "system.webServer" section of a web.config file from code behind?

Im getting null value when using:

ConfigurationSection test = (ConfigurationSection)config.GetSection("system.webServer/httpErrors");

I would like to change the value of errorMode and existingResponse. Also remove any present "remove" or "error" declerations nested inside of "httpErrors".

From this:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" path="/ErrorPages/NotFound.aspx" />
</httpErrors>

To this:

<httpErrors errorMode="Detailed" existingResponse="Auto" />

You can use this code:

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();

For more info look at Change a web.config programmatically with C# (.NET)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM