c# - 引发异常时不显示自定义错误页面

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

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

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

Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.



错误显示我的设置摘要,如果有人想查看更多内容,请询问。仅供引用,我通过从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(此 Controller 内没有断点被命中)
public ViewResult Index()
{
    return View("Error");
}

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

最佳答案

您是否在web.config中指定了所有可用的根目录?似乎您的 Controller 只是具有索引操作,返回了一些 View ,但它应该也具有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
);

关于c# - 引发异常时不显示自定义错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988908/

相关文章:

c# - 如何从 JavaScript 方法重定向到操作?

jquery - 从我的服务器发送的静态内容(例如 : . css、.js)是否被 gzip 压缩?

c# - 从 Unity 脚本调用 Android 电子邮件 Intent

c# - 根据另一个控件的属性绑定(bind)列表中的数据

c# - 泛型类型转换

c# - 容错 XML 阅读器

.net - 时间作为决定性值(value)

c# - asp.net MVC 4 模型 [Key] 属性无法识别

c# - 数据未从 View (ajax)发送到 Controller (c#)

c# - asp.net oledb命令返回所有行