如何解决从后面的代码编辑web.config中的httpErrors部分
是否可以从后面的代码中编辑web.config文件的“ system.webServer”部分中的“ httpErrors”?
使用时,我得到空值
ConfigurationSection test = (ConfigurationSection)config.GetSection("system.webServer/httpErrors");
我想更改errorMode和existingResponse的值。此外,请删除嵌套在“ httpErrors”内部的所有现有“删除”或“错误”文件。
从此:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" path="/ErrorPages/NotFound.aspx" />
</httpErrors>
对此:
<httpErrors errorMode="Detailed" existingResponse="Auto" />
解决方法
您可以使用以下代码:
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();
有关更多信息,请查看Change a web.config programmatically with C# (.NET)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。