I've spent a lot of time trying to figure out a workaround for this to no avail, so I thought I'd see if anyone here has an idea.
I'm using Elmah in my ASP.NET MVC3 application. I'm using the exact same code from the accepted answer in the previous link.
I also have this code in my Global.asax for displaying error pages with the correct HTTP response:
/// <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);
}
The problem occurs when I'm not on my (local) development machine (which initially made me think it was customErrors related). When an exception is thrown, Elmah handles the error and logs it correctly. I also end up on the correct error page. However, before ending up on the correct error page, I can see another intermediate exception being logged:
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 is trying to load up a default error page even though I am trying to handle it. Does anyone have any ideas on how to prevent this?
Don't call the base.OnException(context);
method in your custom error handler that derives from HandleErrorAttribute
. You no longer need it because you have implemented a custom error handling in Application_Error
.