【问题标题】:Custom error page isn't shown when exception thrown抛出异常时不显示自定义错误页面
【发布时间】:2013-08-02 01:43:59
【问题描述】:

我已经设置了一个自定义错误处理程序,以便在发生崩溃时向用户显示诊断信息。问题是未显示自定义错误页面,并且在尝试显示错误页面时抛出异常(根据网页)。我无法弄清楚是什么原因造成的?我为 500 和 404 错误设置了类似的页面,它们可以正常工作。

错误页面显示Server Error in '/' Application.

描述:处理您的请求时发生异常。此外,在执行第一个异常的自定义错误页面时发生了另一个异常。请求已终止。

我会展示我的设置的 sn-ps,如果有人想看更多,请询问。仅供参考,我通过从我的web.config 中删除连接字符串详细信息来引发异常(这是我们在部署期间看到的实际错误,所以我想专门针对这个)

<system.webServer>
  <httpErrors errorMode="Custom">
    <remove statusCode="404" />
    <error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
    <remove statusCode="403" />
    <error statusCode="403" path="/error/forbidden" responseMode="ExecuteURL" />
    <remove statusCode="500" />
    <error statusCode="500" path="/error/" responseMode="ExecuteURL" />
  </httpErrors>
</system.webServer>

<!-- .... -->

<system.web>
  <customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRewrite">
    <error redirect="~/Error/NotFound" statusCode="404" />
    <error redirect="~/Error/Forbidden" statusCode="403"  />
    <error redirect="~/Error/" statusCode="500"  />
  </customErrors>
</system.web>

然后我有一个带有默认 Index() 函数的 ErrorController(此控制器内没有断点被命中)

public ViewResult Index()
{
    return View("Error");
}

注意:我有 Forbidden()NotFound() 的函数 - 我只是没有在此处复制它们 - 这些错误可以正常工作。

【问题讨论】:

  • 能否发布错误详情?

标签: c# .net asp.net-mvc error-handling custom-error-pages


【解决方案1】:

您是否拥有在 web.config 中指定的所有可用根?似乎您的控制器只有索引操作,返回一些视图,但它应该也有 NotFoundForbidden 操作,或者至少是正确的路由,例如:

routes.MapRoute(
    "NotFound",
    "Error/NotFound",
    new { controller = "Error", action = "Index" }, //bind all routes to single action
    null, controllerNamespaces
);
routes.MapRoute(
    "Forbidden",
    "Error/Forbidden",
    new { controller = "Error", action = "Index" }, //bind all routes to single action
    null, controllerNamespaces
);

【讨论】:

    【解决方案2】:

    如果您没有设置 NotFound 和 Forbidden 操作,则这些错误可能会引发第二个异常,该异常将进入您的索引页面,因为这是默认设置。尝试将另外两个添加到您的控制器中,看看它们是否被击中,也许您可​​以更轻松地找到问题。

    还要注意 ResponseRewrite 与 mvc 不兼容,请在此处查看答案:CustomErrors does not work when setting redirectMode="ResponseRewrite"

    【讨论】:

      【解决方案3】:

      重定向到另一个动作时不能使用redirectMode="ResponseRewrite",必须使用redirectMode="ResponseRedirect"。你将失去使用 Server.GetLastError() 的能力,但如果你不需要它,你会没事的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-15
        • 2014-01-30
        • 1970-01-01
        • 2015-03-20
        • 2011-09-18
        • 2013-11-13
        • 1970-01-01
        • 2015-08-31
        相关资源
        最近更新 更多