我已经设置了一个自定义错误处理程序,以在发生崩溃时向用户显示诊断信息。问题是自定义错误页面未显示,并且在尝试显示错误页面时抛出异常(根据网页)。我不知道是什么原因造成的?我为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 ,但它应该也具有NotFound
和Forbidden
操作,或者至少具有正确的溃败,例如:
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/