【问题标题】:Edit httpErrors section in web.config from code behind从后面的代码编辑 web.config 中的 httpErrors 部分
【发布时间】:2020-12-20 07:05:48
【问题描述】:

是否可以从后面的代码编辑 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" />

【问题讨论】:

标签: c# web-config code-behind


【解决方案1】:

您可以使用此代码:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2012-11-20
    • 2016-11-13
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多