title: How to implement proper HTTP error handling in .NET MVC 2?
body: <p>I've been struggling all day to implement error handling in my ASP.NET MVC 2 app. I've looked at a variety of techniques, but none work properly. I'm using MVC2 and .NET 4.0 (started the project before MVC3 was released; we'll upgrade after we deliver our initial release).</p>
  
  <p>At this point, I'll be happy to properly handle 404 and 500 errors -- 403 (authorization required) would be great, too, followed by various other specific responses. Right now, I either get all 404s, all 500s, all 302s before the 404, or all 302s before the 500.</p>
  
  <p>Here are my requirements (which should be pretty close to the basic requirements of HTTP):</p>
  
  <ul>
  <li><p>If a resource is not found, throw a 404, and display a 404-specific page with the requested URL. DO NOT return an intermediate response code like 302. Ideally, keep the requested URL, rather than showing a new URL like <code>/Error/NotFound</code> -- but if the latter displays, be sure we didn't return a redirect response to get it.</p></li>
  <li><p>If an internal server error occurred, throw a 500, and display a 500-specific error with some indication of what went wrong. Again, don't return an intermediate response code, and ideally don't change the URL.</p></li>
  </ul>
  
  <p>Here's what I'd consider a 404:</p>
  
  <ol>
  <li>Static file not found: <code>/Content/non-existent-dir/non-existent-file.txt</code></li>
  <li>Controller not found: <code>/non-existent-controller/Foo/666</code></li>
  <li>Controller found, but Action not found: <code>/Home/non-existent-action/666</code></li>
  <li>Controller and action found, but the action can't find the requested object: <code>/Home/Login/non-existent-id</code></li>
  </ol>
  
  <p>Here's what I'd consider a 500:</p>
  
  <ol start="5">
  <li>Post a bad value: <code>POST /User/New/new-user-name-too-long-for-db-column-constraint</code></li>
  <li>Non-data-related problem, like a Web Service endpoint not responding</li>
  </ol>
  
  <p>Some of these problems need to be identified by specific controllers or models, and then the controllers should throw the appropriate HttpException. The rest should be handled more generically.</p>
  
  <p>For 404 case #2, I tried to use a custom ControllerFactory to throw a 404 if the controller can't be found. 
  For 404 case #3, I've tried to use a custom base controller to override <code>HandleUnknownAction</code> and throw a 404. </p>
  
  <p>In both cases, I get a 302 before the 404. And, I never get 500 errors; if I modify Web.config to put a typo in my Web Service endpoint, I still get a 302, then a 404 saying the URL (controller/action) which <em>uses</em> the Web Service can't be found.
  I also get the requested URL as a(n unwanted) querystring param: <code>/Error/NotFound?aspxerrorpath=/Home/non-existent-action</code></p>
  
  <p>Both of these techniques came from <a href="http://www.niksmit.com/wp/?p=17" rel="noreferrer">http://www.niksmit.com/wp/?p=17</a> (How to get normal 404 (Page not found) error pages using ASP.Net MVC), pointed to from <a href="http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/" rel="noreferrer">http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/</a></p>
  
  <p>If in Web.config I have <code>&lt;customErrors mode="On" defaultRedirect="~/Error/Unknown" redirectMode="ResponseRedirect" /&gt;</code>, I get the appropriate response code, but my Error controller never gets called. Taking out the <code>redirectMode</code> attribute gets me the MVC error views, but with an intervening 302 and a changed URL -- and always the same controller (<code>Unknown</code> = 500; if I change it to <code>NotFound</code> everything looks like a 404).</p>
  
  <p>Here are some of the other things I've read and tried to implement:</p>
  
  <ul>
  <li><a href="http://www.davidjuth.com/asp-net-mvc-error-handler.aspx" rel="noreferrer">http://www.davidjuth.com/asp-net-mvc-error-handler.aspx</a></li>
  <li><a href="http://sanjayuttam.com/wordpress/index.php/c-sharp/c-sharp-code-examples/error-handling-in-asp-net-mvc-1-part-2-of-2/" rel="noreferrer">http://sanjayuttam.com/wordpress/index.php/c-sharp/c-sharp-code-examples/error-handling-in-asp-net-mvc-1-part-2-of-2/</a></li>
  <li><a href="http://blog.hebbink.com/post/2010/12/14/NET-custom-404-error-page-returns-302-for-http-status.aspx" rel="noreferrer">http://blog.hebbink.com/post/2010/12/14/NET-custom-404-error-page-returns-302-for-http-status.aspx</a></li>
  <li><a href="http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html" rel="noreferrer">http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html</a></li>
  <li><a href="http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx" rel="noreferrer">http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx</a></li>
  </ul>
  
  <p>.. along with a bunch of StackOverflow posts.</p>
  
  <p>Seems to me this sort of error handling is pretty basic to Web apps, and the MVC framework ought to have defaults that do this out of the box, and let people extend it to work otherwise. Perhaps they'll do it in a future release. In the meantime, can someone give me comprehensive details on how to implement proper HTTP responses?</p>