当抛出异常时不显示自定义错误页面
如果发生崩溃,我已经设置了自定义错误处理程序以向用户显示诊断信息。问题是自定义错误页面没有显示,我得到一个异常(根据网页)抛出,同时试图显示错误页面。我无法弄清楚是什么造成的?我有类似的网页设置为500和404错误,工作正常。当抛出异常时不显示自定义错误页面
错误页说Server Error in '/' Application.
说明:处理您的请求时发生异常。此外,执行第一个异常的自定义错误页面时发生另一个异常。该请求已被终止。
生病显示我的设置的片段,如果有人想看到更多请问。仅供参考,我从我的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>
然后我有一个ErrorController具有默认Index()
功能(此控制器内没有断点被击中)
public ViewResult Index()
{
return View("Error");
}
注:我有功能Forbidden()
和NotFound()
- 我只是他们在这里didnt复制 - 这些错误完全正常工作。
您是否拥有您在web.config中指定的所有可用根?看来你的控制器只是有索引操作,返回一些看法,但它应该有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
);
这两个错误页面都能正常工作 – Chris
如果你没有NOTFOUND和禁止行为设置这些错误很可能会抛出第二个异常,因为它是默认的,所以会进入索引页面。试着将这两个人加入你的控制器,看看他们是否被击中,也许你会发现问题更容易。
还要注意的是ResponseRewrite不与MVC兼容,在这里看到了答案:CustomErrors does not work when setting redirectMode="ResponseRewrite"
这些页面/错误代码正常工作 – Chris
重定向到另一个动作时,您不能使用redirectMode =“ResponseRewrite”,你必须使用redirectMode =“ResponseRedirect”。你将失去使用Server.GetLastError()的能力,但是如果你不需要,你会没事的。
你能发布错误细节吗? – Sergio