【问题标题】:How to override HttpErrors section in Web.Config - ASP.NET MVC3如何覆盖 Web.Config 中的 HttpErrors 部分 - ASP.NET MVC3
【发布时间】:2011-08-27 09:09:41
【问题描述】:

我正在尝试根据这个问题的答案添加 custon 404 页面

ASP.NET MVC 404 handling and IIS7 <httpErrors>

通过添加

<httpErrors existingResponse="PassThrough" />

在我的 Web.Config 文件中的 &lt;system.webServer&gt; 标记下。

但我收到以下错误

Module: CustomErrorModule 
Notification: SendResponse 
Handler: System.Web.Mvc.MvcHandler 
Error Code: 0x80070021 
Config Error: This configuration section cannot be used at this path. 
              This happens when the section is locked at a parent level. 
              Locking is either by default (overrideModeDefault="Deny"), 
              or set explicitly by a location tag with overrideMode="Deny" 
              or the legacy allowOverride="false".  


 Config Source
  153:       <httpErrors existingResponse="PassThrough" />
  154:   </system.webServer>

我还尝试覆盖基于 http://learn.iis.net/page.aspx/145/how-to-use-locking-in-iis-70-configuration 的锁定(任务 2)

通过在 Web.config 中添加位置标记

如下

<configuration>
....
....

  <location  allowOverride="true">
        <system.webServer>
            <httpErrors existingResponse="PassThrough" />
        </system.webServer>
  </location>
</configuration>

但我遇到了同样的错误。

我应该如何在 Web.config 中配置 httpErrors 元素使其正常工作

我正在使用 IIS 7、VS 2010、ASP.NET MVC3

更新:

我能够摆脱锁定的错误

如果我修改 applicationHost.config 文件并更改

这个

<section name="httpErrors" overrideModeDefault="Deny" />

<section name="httpErrors" overrideModeDefault="Allow" />

但理想情况下我不想更改 applicationHost.config 文件并希望从 Web.config 文件中覆盖它

【问题讨论】:

  • 您是否碰巧尝试了 exisitingResponse="Replace" 而不是“PassThrough”?
  • 只为谷歌;德语错误消息为Dieser Konfigurationsabschnitt kann in diesem Pfad nicht verwendet werden. Dies ist der Fall, wenn der Abschnitt auf übergeordneter Ebene gesperrt ist. Die Sperrung erfolgt standardmäßig (overrideModeDefault="Deny") oder wird explizit mit einem location-Tag mit overrideMode="Deny" oder der Legacyeinstellung allowOverride="false" festgelegt.
  • 是否可以将 configSourcehttpErrors 一起使用?

标签: asp.net-mvc-3 iis-7 web-config


【解决方案1】:

如果您有兴趣,可以从 Global.asax.cs 创建自定义错误处理:

protected void Application_Error()
        {

                var exc = Server.GetLastError();
                var httpExc = exc as HttpException;
                Response.Clear();
                Server.ClearError();
                var routeData = new RouteData();
                routeData.Values["controller"] = "Error";
                routeData.Values["action"] = "General";
                routeData.Values["exception"] = exc;
                Response.StatusCode = 500;
                if (httpExc != null)
                {
                    Response.StatusCode = httpExc.GetHttpCode();
                    routeData.Values["action"] = "Http404";
                    routeData.Values["exception"] = httpExc;
                }
                Response.TrySkipIisCustomErrors = true;
                IController errorsController = new WWS_Website.Controllers.ErrorController();
                var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
                errorsController.Execute(rc);

        }

控制器:

public ActionResult General(Exception exception)
{
  return View(exception);
}

public ActionResult Http404(Exception exception)
{
  return View(exception);
}

您可以对每个错误有不同的视图。您还可以通过此控制器方法向您的网站管理员电子邮件帐户发送电子邮件 - 非常方便。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2020-01-23
    • 2014-07-09
    • 1970-01-01
    • 2011-12-23
    相关资源
    最近更新 更多