404 自定义重定向
您好,如果响应是 404,我正在尝试进行重定向,但它没有按预期工作,你们专家能看到这个问题吗? 中的通用 404
它仍然会转到我的 Global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Response.Status == "404 Not Found")
{
string notFound = "~/custom_notfound.aspx";
Response.Redirect(notFound);
}
}
UPDATE
到目前为止已尝试过
(Response.Status == "404 Not Found")
(Response.Status == "404")
(Response.StatusCode == 404)
Hello I am trying to do a redirect if the response is a 404 but it is not working as expected, can you experts see the issue?. It still goes to the generic 404
in my Global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Response.Status == "404 Not Found")
{
string notFound = "~/custom_notfound.aspx";
Response.Redirect(notFound);
}
}
UPDATE
Tried so far
(Response.Status == "404 Not Found")
(Response.Status == "404")
(Response.StatusCode == 404)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用 web.config 自定义错误部分 - 如图所示这里
例如在 system.web 部分中,
You can also use the web.config customerrors section - as shown here
e.g. In the system.web section,
我认为 BeginRequest 无法知道 404 错误。尝试改为实施 Application_Error 。检查 Server.GetLastError() 是否为 HttpException,如果是,请检查状态。
I don't think BeginRequest could know about 404 errors. Try implementing Application_Error instead. Check to see if Server.GetLastError() is an HttpException, and if so, check the status.
您可以添加到 web.config 中来执行此重定向,无需使用
Application_BeginRequest
来处理此问题。请参阅此 服务器故障问题。
如果您无法使用 web.config,那么我会将您的启动页面设置为不存在的页面,在 BeginRequest 中放置一个断点,调试应用程序并查看请求以了解如何确定它是 404。这样就更容易确定最佳解决方案。
进一步研究一下,有一个 HttpStatusCode在 HttpWebResponse 类中使用。因此,使用应用程序的不同覆盖来获取默认响应,然后根据枚举检查其状态可能是有意义的。
You could add to your web.config to do this redirection, you don't need to use
Application_BeginRequest
to handle this.See this ServerFault question.
If you can't use the web.config then I would set your startup page to one that doesn't exist, put a breakpoint in your BeginRequest, debug the app and look at the request to see how to determine it is a 404. That would be much easier to determine the optimal solution.
Looking into this some more, there is a HttpStatusCode that is used in the HttpWebResponse class. So it may make sense to use a different override of the Application to get the default response, and then check it's status against the Enum.
您还可以使用 web.config
You can also use the web.config