I’m hosting my site on DiscountASP.NET. 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.
<%@ 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.
<%@ 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 the host’s fault? Thank you.
I think I’ve figured out. Here’s a condensed version of my web.config file:
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.