title: Custom error page not displayed when throwing exception from Attribute
body: <p>Let me first describe how my error handling is set up as there are several ways to do this in ASP.NET MVC. I have configured httpErrors in web.config, overridden HandleUnknownAction in the controller base and set up a route to handle anything undefined and this works fine (e.g. displaying my 404 error page when anything is not found and displaying my 500 error page when something has gone wrong in the code).</p>
  
  <p>Web.config:</p>
  
  <pre><code>  &lt;system.webServer&gt;
      &lt;httpErrors errorMode="Custom" existingResponse="Replace"&gt;
        &lt;clear /&gt;
        &lt;error statusCode="400" path="/error/400" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="401" path="/error/401" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="402" path="/error/402" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="403" path="/error/403" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="404" path="/error/404" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="405" path="/error/405" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="406" path="/error/406" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="407" path="/error/407" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="408" path="/error/408" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="409" path="/error/409" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="410" path="/error/410" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="411" path="/error/411" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="412" path="/error/412" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="413" path="/error/413" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="414" path="/error/414" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="415" path="/error/415" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="416" path="/error/416" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="417" path="/error/417" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="429" path="/error/429" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="500" path="/error/500" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="501" path="/error/501" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="502" path="/error/502" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="503" path="/error/503" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="504" path="/error/504" responseMode="ExecuteURL" /&gt;
        &lt;error statusCode="505" path="/error/505" responseMode="ExecuteURL" /&gt;
      &lt;/httpErrors&gt;
    &lt;/system.webServer&gt;
  </code></pre>
  
  <p>(my custom error page is a view with the route "error/{code}")</p>
  
  <p>Controller base:</p>
  
  <pre><code>protected override void HandleUnknownAction(string actionName)
  {
      throw new HttpException(404, null);
  }
  </code></pre>
  
  <p>RouteConfig:</p>
  
  <pre><code>routes.MapRoute("Undefined", "{*url}", new { controller = "Undefined", action = "Index" });
  </code></pre>
  
  <p>UndefinedController:</p>
  
  <pre><code>public class UndefinedController : Controller
  {
      public ActionResult Index()
      {
          throw new HttpException(404, null);
      }
  }
  </code></pre>
  
  <p>I also have a HttpErrorResult (ActionResult) class which works fine to return any error:</p>
  
  <pre><code>public class HttpErrorResult : ActionResult
  {
      private int _code = 0;
  
      public HttpErrorResult(int code)
      {
          _code = code;
      }
  
      public override void ExecuteResult(ControllerContext context)
      {
          throw new HttpException(_code, null);
      }
  }
  </code></pre>
  
  <p>Ok that should be all the code involved in my error handling.</p>
  
  <p>The problem I have encountered is that when I return the HttpErrorResult from an Attribute it does not display my custom error page but instead the standard yellow error page saying "Server Error in '/' Application" "Exception of type 'System.Web.HttpException' was thrown."</p>
  
  <p>Specifically I get this problem when I try to return a 429 error (too many requests) in my EnforceRequestLimitAttribute (AuthorizeAttribute) class:</p>
  
  <pre><code>public override void OnAuthorization(AuthorizationContext filterContext)
  {
      if (!AuthorizeCore(filterContext.HttpContext))
          filterContext.Result = new HttpErrorResult(429);
      else
          base.OnAuthorization(filterContext);
  }
  </code></pre>
  
  <p>I cannot understand the reason for this as it works perfectly fine to throw an HttpException in the HandleUnknownAction method and in the UndefinedController and to use the HttpErrorResult in any other controller to get the custom error page but in an Attribute it behaves differently.</p>
  
  <p>Why is this and what can be done to get around it?</p>