22

Problem

when I manually set the HTTP Status of my response stream to, say, 404 or 503, IIS renders up the stock IIS content/view, instead of my custom view.

When I do this with the web development server (AKA. Cassini), it works correctly (that is, my content is displayed and the response.statuscode == my entered data).

Is there any way I can override this behaviour?

How To Replicate

Make a default ASP.NET MVC1 web application. Add the following route

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{*catchall}",
        new { controller = "Home", action = "Index" }
        );

}

Now replace the the HomeController's Index method with...

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        Response.StatusCode = 404;
        return View();
    }
}
Pure.Krome
  • 6,338
  • 17
  • 72
  • 86

3 Answers3

21

Ok - found the answer. As I expected, IIS is hijacking my non 200 responses. Not sure (ie. I'm not sure if this is the default behaviour OR it's because of a setting one of the team members updated in the machine config, etc...).

Anyways, the key here is tell IIS to not handle any non-200 status result resources.

How? Config entry in the web.config.

<system.webServer>
    <httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough"/>
    .... snipped other IIS relevant elements ... 
</system.webServer>

Now, the key here is existingResponse="PassThrough" . That bad boy tells IIS to leave my resources alone if the HTTP status code != 200.

Want more info? Sure: Read More about this Element on the Official IIS Website.

Pure.Krome
  • 6,338
  • 17
  • 72
  • 86
4

Another way to bypass this is to run the following code in your ASP application:

Response.TrySkipIisCustomErrors = true;

Source: https://stackoverflow.com/a/21271085/238753

Sam
  • 147
  • 1
  • 7
-2

Be carefull with that approach in general. You should NOT render a view on 404 status.

  • I think when a error status code is returned, IIS returns the status error page that is registered with it - not the output from processing. So, you can put a HTML page there (or a link to an aspx page). http://professionalaspnet.com/archive/2008/02/13/Enforcing-a-Custom-404-Page-in-ASP.NET.aspx has a nice explanation how to set up an error page.

  • But that is irrelvant. Quite a number of browsers by default do NOT show that output, but something set in the browser. So, if you rely on people seeing your 404 page - that may not happen. They may see the 404 page that is set up in the browser for them.

TomTom
  • 50,857
  • 7
  • 52
  • 134
  • 1
    Why shouldn't i be rendering a view on a 404? i want a custom page, and my page might require some logic (instead of a static html error page). – Pure.Krome Mar 18 '10 at 14:40
  • ecause the MVC model does not integrate with the IIS error page model. That simple. You ahve to go back to the IIS model for that. Register a page URL for the 404 case. Put some dynamic page there - I am not sure whether this actually COULD point back to a MVC url, but it MUSt originate in the 404 page registration. – TomTom Mar 18 '10 at 14:55
  • Incorrect. Most browsers will render the content of a page returned with a 404 HTTP status. Not all, but most. Also, IIS by default will respect the status code as set by any code in a .NET site, as long as no other modules are overriding that later in the pipeline. See the question author's answer on how that was happening in this case. – Mufasa Mar 19 '10 at 22:47