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();
}
}
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.
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.
Another way to bypass this is to run the following code in your ASP application:
Source: https://stackoverflow.com/a/21271085/238753
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.