简体   繁体   English

ASP.NET 404处理代码在本地工作,但不能在服务器上工作

[英]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.

相关问题 仅在 UAT web 服务器中部署时出现路由错误 404。 在当地工作。 ASP.NET核心 - Routing error 404 only when deployed in UAT web server. Works locally. ASP.NET Core ASP.NET Active Directory身份验证在本地工作,但不能在服务器上工作 - ASP.NET Active Directory Authentication works locally, but not on server ASP.Net 页面在本地工作但在推送到服务器时失败 - ASP.Net Page Works Locally But Fails When Pushed To Server Asp.Net html.renderaction在本地工作,但在服务器上没有错误 - Asp.Net html.renderaction works locally but not on server no error 在 ASP.NET Core 2 中使用 UseExceptionHandler() 和 API 处理和 404 处理 - using UseExceptionHandler() with API handling and 404 handling in ASP.NET Core 2 邮件发送代码在本地运行,但在ASP.NET MVC中的服务器上不运行 - Mail sending code works at local but not on server in asp.net mvc ASP.NET MVC 4-RouteConfig.cs-本地更新,可以工作-在服务器上不更新 - ASP.NET MVC 4 - RouteConfig.cs - updated locally, works - doesn't update on server ASP.NET 项目在 VS 运行时在本地运行,但不在本地计算机或服务器上的 IIS 上运行 - ASP.NET project works locally on build when VS is running but not on IIS on my local machine or my server asp.net 404 - 在网站上找不到的文件或目录,但在本地它可以 - asp.net 404 - File or directory not found on website but locally it can ASP.NET应用程序可在localhost服务器上运行,并在远程返回403和404错误 - ASP.NET application works on localhost server, on remote returns 403 and 404 errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM