如何以编程方式更改 web.config 文件中的自定义错误模式?我需要更改以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections />
...
<system.web>
....
<customErrors mode="RemoteOnly">
<error statusCode="404" redirect="~/errors/404" />
</customErrors>
到
<customErrors mode="off">
<error statusCode="404" redirect="~/errors/404" />
</customErrors>
有人对此有任何想法吗?
更新
作为CoDe aDDict 的回答,我尝试使用它:
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
CustomErrorsSection CustomErrorsection = (CustomErrorsSection)config.GetSection("system.web/customErrors");
CustomErrorsection.Mode = CustomErrorsMode.Off;
config.Save();
但有一个例外:
Access to the path 'D:\Projects\MyProject\web.config' is denied.