简体   繁体   English

从后面的代码编辑 web.config 中的 httpErrors 部分

[英]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?是否可以从后面的代码编辑 web.config 文件的“system.webServer”部分中的“httpErrors”?

Im getting null value when using:使用时我得到 null 值:

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

I would like to change the value of errorMode and existingResponse.我想更改 errorMode 和 existingResponse 的值。 Also remove any present "remove" or "error" declerations nested inside of "httpErrors".同时删除嵌套在“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)有关详细信息,请参阅使用 C# (.NET) 以编程方式更改 web.config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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