title: Error pages not found, throwing ELMAH error with Custom Error Pages
body: <p>I've made some modifications to Global.asax so that I can show custom error pages (403, 404, and 500) Here's the code:</p>
  
  <pre><code>    public class MvcApplication : System.Web.HttpApplication
      {
          protected void Application_Start()
          {
              AreaRegistration.RegisterAllAreas();
  
              WebApiConfig.Register(GlobalConfiguration.Configuration);
              //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
              RouteConfig.RegisterRoutes(RouteTable.Routes);
              BundleConfig.RegisterBundles(BundleTable.Bundles);
          }
  
          protected void Application_Error(object sender, EventArgs e)
          {
              if (Context.IsCustomErrorEnabled)
              {
                  ShowCustomErrorPage(Server.GetLastError());
              }
          }
  
          private void ShowCustomErrorPage(Exception exception)
          {
              HttpException httpException = exception as HttpException;
              if (httpException == null)
              {
                  httpException = new HttpException(500, "Internal Server Error", exception);
              }
  
              Response.Clear();
              RouteData routeData = new RouteData();
              routeData.Values.Add("controller", "Error");
              routeData.Values.Add("fromAppErrorEvent", true);
  
              switch (httpException.GetHttpCode())
              {
                  case 403:
                      routeData.Values.Add("action", "AccessDenied");
                      break;
  
                  case 404:
                      routeData.Values.Add("action", "NotFound");
                      break;
  
                  case 500:
                      routeData.Values.Add("action", "ServerError");
                      break;
  
                  default:
                      routeData.Values.Add("action", "DefaultError");
                      routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
                      break;
              }
  
              Server.ClearError();
  
              IController controller = new ErrorController();
              controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
          }
      }
  </code></pre>
  
  <p>I've also added the following to my Web.Config:</p>
  
  <pre><code>&lt;customErrors mode="On"&gt;
      &lt;!-- There is custom handling of errors in Global.asax --&gt;
  &lt;/customErrors&gt;
  </code></pre>
  
  <p>The custom error pages show up correctly, and ELMAH will correctly log the error that was (purposefully) thrown. But ELMAH also catches and logs an additional error:</p>
  
  <pre><code>System.InvalidOperationException: The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/account/Error.aspx ~/Views/account/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/account/Error.cshtml ~/Views/account/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml
  </code></pre>
  
  <p>My first instincts led me to disabling the global <code>HandleErrorAttribute</code> in the filter configuration. And, similar SO questions such as:<a href="https://stackoverflow.com/questions/7265193/mvc-problem-with-custom-error-pages">MVC problem with custom error pages</a> led me to believe my suspicions were right. But even after disabling the global <code>HandleErrorAttribute</code> I am still getting the Error that the Error view could not be found! What gives? My only other hunch is that my base controller derives from <code>System.Web.Mvc.Controller</code>  I tried to examine the source to see if the <code>HandleErrorAttribute</code> is applied to <code>System.Web.Mvc.Controller</code> but couldn't glean anything...</p>
  
  <p>UPDATE: 
  I tried overriding my base controller to mark exceptions as handled like this:</p>
  
  <pre><code>protected override void OnException(ExceptionContext filterContext)
  {
      filterContext.ExceptionHandled = true;
      base.OnException(filterContext);
  }
  </code></pre>
  
  <p>but that didn't solve the problem.</p>
  
  <p>UPDATE2:
  I placed an Error.aspx file into the shared views, just to see what would happen. When it's there, ELMAH logs the forced exception, and then the shared view is served up - it never reaches <code>Application_Error()</code>   .... not too sure what to make of it.</p>