无法 return ASP.NET 中自定义错误页面的 HTTP 状态 404
Can't return HTTP status 404 for custom error page in ASP.NET
我们的一个基于经典 ASP.NET 技术的遗留网站已移至新主机。现在它是一个云 运行 Windows Azure Pack,我们的站点在 .NET Framework 4.6.1 下 运行。
但是,我仍然无法 return 具有正确 HTTP 状态 404 的不存在的 .aspx 资源的自定义错误页面 - 状态始终为 302。自定义 404 错误页面最初已定义像这样 web.config:
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="/error/server-error.aspx">
<error statusCode="404" redirect="/error/resource-not-found.aspx"/>
</customErrors>
</system.web>
我确实阅读了诸如 this 之类的 SO 帖子并尝试了建议的答案,包括将 redirectMode 设置为 ResponseRewrite,但没有任何帮助或没有按预期工作。
我是不是漏掉了什么,或者有其他解决方案吗?
我没有对 .aspx 做太多
我只是在谷歌上搜索,我想知道你是否有
<% Response.StatusCode = 404 %>
在你的 404.aspx 页面之上 ?
However, I still can't return a custom error page for a non-existing .aspx resource with the correct HTTP status 404 - the status is always 302.
据我所知,HTTP 302意味着请求的资源已被移动到不同的位置。根据您的描述,我创建了一个 ASP.NET Web 应用程序并使用您提供的自定义错误配置将其部署到 Azure Web App。
这是我的详细测试:
关闭CustomErrors模式
如果找不到您请求的资源,那么您可以通过浏览器中的 Fiddler 或 Developer Tools 获得如下响应header。
设置CustomErrors的模式为RemoteOnly
根据您提供的配置,当没有找到请求的资源时,您可以得到如下详细信息:
经过测试,我这边和Azure Web App都测试成功了。
当您通过 Fiddler 收到 302 响应时,请尝试检查位置 header。另外,如果你希望当找不到资源时,浏览器会重定向到resource-not-found.aspx页面并返回404状态码,你可以按照这个相关的thread.
我已经设法克服了这个问题。首先,我关闭了 web.config:
错误 404 的重定向
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="/error/server-error.aspx">
<!-- <error statusCode="404" redirect="/error/resource-not-found.aspx"/> -->
</customErrors>
</system.web>
然后我将以下 Application_Error 事件处理程序添加到我的 Global.asax:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim serverError As HttpException = DirectCast(Server.GetLastError(), HttpException)
If Not serverError Is Nothing Then
If 404 = serverError.GetHttpCode() Then
Server.ClearError()
Server.Transfer("/error/resource-not-found.aspx")
End If
End If
End Sub
最后,我将下面的行添加到我的错误 404. 页面 (resource-not-found.aspx):
Sub Page_Load()
' If you're running under IIS 7 in Integrated mode set,
' use this line to override IIS errors:
Response.TrySkipIisCustomErrors = True
Response.StatusCode = 404
Response.StatusDescription = "Page not found"
End Sub
仅供参考:如果您使用 MasterPage 技术,则只会重定向 MasterPage 的可变部分。在我们网站的各个部分尝试了不存在的资源后,我发现这种行为很聪明,对我们的最终用户很有用。
还有一件事。要 return 非 ASPX 资源的 HTTP 404 状态代码,我在 web.config 中使用以下设置:
<system.webServer>
<httpErrors errorMode="Custom" defaultResponseMode="Redirect">
<remove statusCode="404"/>
<error statusCode="404" path="/error/resource-not-found.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
注意404错误的responseMode设置为'ExecuteURL'
我们的一个基于经典 ASP.NET 技术的遗留网站已移至新主机。现在它是一个云 运行 Windows Azure Pack,我们的站点在 .NET Framework 4.6.1 下 运行。
但是,我仍然无法 return 具有正确 HTTP 状态 404 的不存在的 .aspx 资源的自定义错误页面 - 状态始终为 302。自定义 404 错误页面最初已定义像这样 web.config:
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="/error/server-error.aspx">
<error statusCode="404" redirect="/error/resource-not-found.aspx"/>
</customErrors>
</system.web>
我确实阅读了诸如 this 之类的 SO 帖子并尝试了建议的答案,包括将 redirectMode 设置为 ResponseRewrite,但没有任何帮助或没有按预期工作。
我是不是漏掉了什么,或者有其他解决方案吗?
我没有对 .aspx 做太多 我只是在谷歌上搜索,我想知道你是否有
<% Response.StatusCode = 404 %>
在你的 404.aspx 页面之上 ?
However, I still can't return a custom error page for a non-existing .aspx resource with the correct HTTP status 404 - the status is always 302.
据我所知,HTTP 302意味着请求的资源已被移动到不同的位置。根据您的描述,我创建了一个 ASP.NET Web 应用程序并使用您提供的自定义错误配置将其部署到 Azure Web App。
这是我的详细测试:
关闭CustomErrors模式
如果找不到您请求的资源,那么您可以通过浏览器中的 Fiddler 或 Developer Tools 获得如下响应header。
设置CustomErrors的模式为RemoteOnly
根据您提供的配置,当没有找到请求的资源时,您可以得到如下详细信息:
经过测试,我这边和Azure Web App都测试成功了。 当您通过 Fiddler 收到 302 响应时,请尝试检查位置 header。另外,如果你希望当找不到资源时,浏览器会重定向到resource-not-found.aspx页面并返回404状态码,你可以按照这个相关的thread.
我已经设法克服了这个问题。首先,我关闭了 web.config:
错误 404 的重定向<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="/error/server-error.aspx">
<!-- <error statusCode="404" redirect="/error/resource-not-found.aspx"/> -->
</customErrors>
</system.web>
然后我将以下 Application_Error 事件处理程序添加到我的 Global.asax:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim serverError As HttpException = DirectCast(Server.GetLastError(), HttpException)
If Not serverError Is Nothing Then
If 404 = serverError.GetHttpCode() Then
Server.ClearError()
Server.Transfer("/error/resource-not-found.aspx")
End If
End If
End Sub
最后,我将下面的行添加到我的错误 404. 页面 (resource-not-found.aspx):
Sub Page_Load()
' If you're running under IIS 7 in Integrated mode set,
' use this line to override IIS errors:
Response.TrySkipIisCustomErrors = True
Response.StatusCode = 404
Response.StatusDescription = "Page not found"
End Sub
仅供参考:如果您使用 MasterPage 技术,则只会重定向 MasterPage 的可变部分。在我们网站的各个部分尝试了不存在的资源后,我发现这种行为很聪明,对我们的最终用户很有用。
还有一件事。要 return 非 ASPX 资源的 HTTP 404 状态代码,我在 web.config 中使用以下设置:
<system.webServer>
<httpErrors errorMode="Custom" defaultResponseMode="Redirect">
<remove statusCode="404"/>
<error statusCode="404" path="/error/resource-not-found.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
注意404错误的responseMode设置为'ExecuteURL'