ASP.NET 404 Handling Code Works Locally but Not on Server

Discussion in 'ASP.NET / ASP.NET Core' started by nocturnus, Dec 6, 2012.

  1. I've been having problems with my 404 error handling recently because the client is not being sent the 404 status code, and I can't figure out why it started up out of the blue. It used to work. I've tried working around my error handling code by starting with a simple example. I have two pages, both of which exist:

    404_start.aspx: This page is meant to emulate a non-existent page.
    HTML:
    <%@ Page Language="C#" Debug="True" Strict="True" %>
    <script runat="server">
    public void Page_Load()
    {
        Response.TrySkipIisCustomErrors = true;
        Response.StatusCode = 404;
        Response.Status = "404 Not Found";
        Server.ClearError();
        Server.Transfer("404_end.aspx");
    }
    </script>
    <html>
    <body>
    Start
    <br />Response.StatusCode:  <%=Response.StatusCode%>
    <br />Response.Status:  <%=Response.Status%>
    </body>
    </html>
    404_end.aspx: This page is meant to emulate the error message the user sees.
    HTML:
    <%@ Page Language="C#" Debug="True" Strict="True" %>
    <html>
    <body>
    End
    <br />Response.StatusCode:  <%=Response.StatusCode%>
    <br />Response.Status:  <%=Response.Status%>
    <br />This is extra text to fix this bug in Internet Explorer:  http://queenofsubtle.com/404/?page_id=2158
    <br />This is extra text to fix this bug in Internet Explorer:  http://queenofsubtle.com/404/?page_id=2158
    <br />This is extra text to fix this bug in Internet Explorer:  http://queenofsubtle.com/404/?page_id=2158
    <br />This is extra text to fix this bug in Internet Explorer:  http://queenofsubtle.com/404/?page_id=2158
    </body>
    </html>
    So the start page redirects to the end page, but the 404 error never comes through. Fiddler says it's a 302 followed by a 200. But the 404_end.aspx page actually reads, "Response.StatusCode: 404." Locally, Fiddler sees a 404 error as desired. Could it be DiscountASP.NET's fault? Thank you.
     
  2. I think I've figured out. Here's a condensed version of my web.config file:
    HTML:
    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <customErrors mode="On" defaultRedirect="~/error/default.aspx" redirectMode="ResponseRewrite">
          <error statusCode="404" redirect="~/error/404.aspx" />
        </customErrors>
      </system.web>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Redirect to HTTPS" stopProcessing="true">
              <match url=".*" />
              <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
                <add input="{HTTP_HOST}" pattern="192\.168\." negate="true" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}{PATH_INFO}" redirectType="SeeOther" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    It seems the rewrite rules and the customErrors were interacting. When I removed the rewrite rule shown above (which is meant to force https when a user requests http), the server started returning 404 errors again. So I implemented the http->https switch in C# instead of the web.config file, and everything seems to work great. Since the http->https switch was performed on the Web, the problem wasn't manifesting itself locally. The problem was evidently not DiscountASP.NET's fault, although it makes no sense to me why I shouldn't be able to use the rewrite rule with the customErrors tag.
     

Share This Page