我花了很多时间试图找出解决方法无济于事,所以我想我会看看这里是否有人有想法。
我在我的 ASP.NET MVC3 应用程序中使用 Elmah。我正在使用与上一个链接中接受的答案完全相同的代码。
我的 Global.asax 中也有这段代码,用于显示带有正确 HTTP 响应的错误页面:
/// <summary>
/// The customErrors functionality provided by ASP.NET results in HTTP 302 redirects occurring which doesn't accurately reflect what the real HTTP code of the response was.
/// This method can be used to handle specific HTTP codes without an intermediate redirect.
/// </summary>
protected void Application_Error() {
var exception = Server.GetLastError();
var httpException = exception as HttpException;
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = "Error500";
Response.StatusCode = 500;
if (httpException != null) {
Response.StatusCode = httpException.GetHttpCode();
Response.TrySkipIisCustomErrors = true;
switch (Response.StatusCode) {
case 403:
routeData.Values["action"] = "Error403";
break;
case 404:
routeData.Values["action"] = "Error404";
routeData.Values["message"] = httpException.Message;
break;
case 500:
routeData.Values["action"] = "Error500";
break;
}
}
IController errorsController = new ErrorController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
当我不在我的(本地)开发机器上时会出现问题(最初让我认为它与customErrors相关)。当抛出异常时,Elmah 会处理错误并正确记录它。我也最终出现在正确的错误页面上。但是,在到达正确的错误页面之前,我可以看到另一个中间异常被记录:
The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Articles/Error.aspx ~/Views/Articles/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/Articles/Error.cshtml ~/Views/Articles/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml
ASP.NET 正在尝试加载默认错误页面,即使我正在尝试处理它。有人对如何防止这种情况有任何想法吗?