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?
If you use Elmah.mvc, then it will help to change this settings to true:
I was having this same issue, but i wasn't executing base.OnException() anywhere. Another possible solution was to remove this from
<system.web>
in my web.config:If you use Elmah.MVC and want to use custom error pages, just change the below value to true in your Web.config:
This will keep Elmah logging enabled but stop it from trying to redirect to the default error page.
Don't call the
base.OnException(context);
method in your custom error handler that derives fromHandleErrorAttribute
. You no longer need it because you have implemented a custom error handling inApplication_Error
.Somewhere your site is trying to navigate to ~/Error and it can't find it because it doesn't exist.
Trying removing or disabling customErrors in your Web.config. I'm willing to be its set to the default of on and redirect to ~/Error
If you want to post your Web.config (omitting any sensitive information) I can probably help. I've dealt with similar issues with Elmah recently.