[英]ASP.NET 404 Handling Code Works Locally but Not on Server
I'm hosting my site on DiscountASP.NET. 我将网站托管在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. 我最近在404错误处理方面遇到了问题,因为没有向客户端发送404状态代码,而且我不知道为什么它突然出现。 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. 404_start.aspx:此页面旨在模拟不存在的页面。
<%@ 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. 404_end.aspx:此页面用于模拟用户看到的错误消息。
<%@ 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. 因此,起始页面重定向到结束页面,但是404错误永远不会发生。 Fiddler says it's a 302 followed by a 200. But the 404_end.aspx page actually reads, "Response.StatusCode: 404." Fiddler说它是302,然后是200。但是404_end.aspx页实际上显示为“ Response.StatusCode:404”。 Locally, Fiddler sees a 404 error as desired. Fiddler在本地看到所需的404错误。 Could it be the host's fault? 可能是主机的错吗? Thank you. 谢谢。
Yes probably. 应该是。
If fiddler is returning the error code, the application will pick the same. 如果提琴手返回错误代码,则应用程序将选择相同的代码。
I think I've figured out. 我想我已经知道了。 Here's a condensed version of my web.config file: 这是我的web.config文件的精简版本:
<?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. 似乎重写规则和customErrors正在交互。 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. 当我删除上面显示的重写规则(当用户请求http时强制使用https)时,服务器开始再次返回404错误。 So I implemented the http->https switch in C# instead of the web.config file, and everything seems to work great. 所以我用C#而不是web.config文件实现了http-> https开关,一切似乎都很好。 Since the http->https switch was performed on the Web, the problem wasn't manifesting itself locally. 由于http-> https切换是在Web上执行的,因此问题不在本地显示。 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. 这个问题显然不是DiscountASP.NET的错,尽管对我来说没有意义,为什么我不应该使用customErrors标记重写规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.