0

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" />
David
  • 141
  • 9

1 Answers1

0

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)

binick
  • 44
  • 3