MVC: 404 route does not work properly in production

I have the following route at the bottom of my global.asax:

//404 ERRORS:
routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "Error", action = "PageNotFound" }
);

Which works fine in Visual Studio, but during production I get a page with an IIS error.

Should this route not catch any URL that was not discovered by others, so there is no 404 in terms of IIS? Is there anything else I need to do in web.config?

Note. I do not want to redirect the 404 URL; rather, I serve the 404 error page at the requested URL (I believe this is the right approach in terms of usability).

UPDATE
In my error controller, I install Response.StatusCode = 404;what seems to be the problem. When I delete this and deploy it back into production, I get my pages again with a friendly error. However, I believe that I need the 404 status in the HTTP header - for SEO purposes - so my question now becomes this:

REVISED QUESTION
How / why IIS intercepts the response and sends its original 404 error and how to prevent this?

** Decision **
Dommer receives a prize for an offer Response.TrySkipIisCustomErrors=true;that (I think) was needed. But there were two more key details:

  • user errors should be included in web.config (duh!) and
  • action 404 must have the [HandleErrors] attribute.


URL- , "404-PageNotFound", , 404, :

[HandleError]
public ActionResult NotFound()
{
    Response.StatusCode = 404;
    Response.TrySkipIisCustomErrors = true;     
    return View("PageNotFound", SearchUtilities.GetPageNotFoundModel(HttpContext.Request.RawUrl));          
}

, , , :

return NotFound();

: RedirectToAction()

:
, , URL- 404.

+5
2

, . - :

// This will match too many things, so let constrain it to those we know are valid
   routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new { controller = "Home|OtherController|AnotherController|..." } // regular expression matching all valid controllers
);

//404 ERRORS:
routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "Error", action = "PageNotFound" }
);

, : 404 ASP.NET MVC?

EDIT:

IIS, :

Response.StatusCode = 404;
Response.TrySkipIisCustomErrors=true; // add this line

MSDN doc: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.tryskipiiscustomerrors.aspx

+4

,

 routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "account", action = "index", id = UrlParameter.Optional } // Parameter defaults
            );


routes.MapRoute(
                        "404-PageNotFound", // Route name
                        "{*url}", 
                        new { controller = "Error", action = "PageNotFound", id = UrlParameter.Optional } // Parameter defaults
                    );
0

All Articles