IIS is overriding my response content, if I manually set the Response.StatusCode

13,100

Solution 1

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.

Solution 2

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

Share:
13,100

Related videos on Youtube

Chris Canal
Author by

Chris Canal

Updated on September 17, 2022

Comments

  • Chris Canal
    Chris Canal over 1 year

    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();
        }
    }
    
  • Chris Canal
    Chris Canal about 14 years
    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).
  • TomTom
    TomTom about 14 years
    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.
  • Mufasa
    Mufasa about 14 years
    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.
  • Kiquenet
    Kiquenet over 8 years
    httpErrors is like Error Pages -> 500 -> Edit Feature Settings -> "Detailed Errors" ?_http://stackoverflow.com/questions/2640526/detailed-500-er‌​ror-message-asp-iis-‌​7-5_
  • A.R.
    A.R. over 4 years
    aaaahhh.. Good ol' IIS making easy things difficult :)
  • AminFarajzadeh
    AminFarajzadeh about 3 years
    It works. You are a life saverrrrrrrrr