8

I've got customErrors set in my web.config

<customErrors mode="On" defaultRedirect="/Error/GeneralError">
    <error statusCode="404" redirect="/Error/NotFound"/>
</customErrors>

This works fine locally. A 404 throws a 404. On the shared hosting it throws up the standard server 404 page unless I specifically set 404 to point to /Error/NotFound. That's fine. Now it will show the custom 404 page except the response status code is 200. So if I try to throw Response.StatusCode = 404; in my NotFound action in ErrorController like this:

public class ErrorController : Controller
{
    public ActionResult NotFound()
    {
        Response.StatusCode = 404;
        return View();
    }
}

the server throws a status code 500 Internal Server Error but my GeneralError page doesn't show, just a blank white page with no source.

I've tried many different combinations but I can't seem to find how to make it show my custom 404 page along with a 404 response.

Any ideas?

mark123
  • 1,065
  • 1
  • 11
  • 16
  • Is there any harm in allowing it to respond with 200 OK on a NotFound? Perhaps issues with web crawling or google webmaster tools. – mark123 Mar 04 '10 at 03:52
  • Just a thought. Wouldn't it be better to use: defaultRedirect="~/Error/GeneralError" and redirect="~/Error/NotFound" with MVC applications. (Notice the ~ at the start of strings. This will map to the base of the application not the base of the domain... as you may know already.) – jwwishart Mar 05 '10 at 04:07

2 Answers2

21

I found out some interesting information here: http://blog.angrypets.com/2008/03/responsetryskip.html

Response.TrySkipIisCustomErrors = true; 

Setting TrySkipIisCustomErrors to true after the Response.StatusCode = 404; takes care of the issue.

Fateme Mirjalili
  • 762
  • 7
  • 16
mark123
  • 1,065
  • 1
  • 11
  • 16
1

Don't use a customErrors element (except to turn mode="On" for local testing). Instead, add an Application_EndRequest method to your MvcApplication per Marco's Better-Than-Unicorns MVC 404 Answer.

Community
  • 1
  • 1
Edward Brey
  • 40,302
  • 20
  • 199
  • 253