404 自定义重定向

发布于 2024-12-08 10:29:43 字数 509 浏览 0 评论 0原文

您好,如果响应是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不吻晚风 2024-12-15 10:29:43

您还可以使用 web.config 自定义错误部分 - 如图所示这里

例如在 system.web 部分中,

<customErrors mode="On" defaultRedirect="/custom_error.aspx">
    <error statusCode="404" redirect="/custom_notfound.aspx" />
</customErrors>

You can also use the web.config customerrors section - as shown here

e.g. In the system.web section,

<customErrors mode="On" defaultRedirect="/custom_error.aspx">
    <error statusCode="404" redirect="/custom_notfound.aspx" />
</customErrors>
盗琴音 2024-12-15 10:29:43

我认为 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.

扛刀软妹 2024-12-15 10:29:43

您可以添加到 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.

遗弃M 2024-12-15 10:29:43

您还可以使用 web.config

<system.webServer>
  <httpErrors errorMode="Custom" defaultResponseMode="File" >
     <remove statusCode="404" />
     <remove statusCode="500" />
     <error statusCode="404" path="404.html" />
     <error statusCode="500" path="500.html" />
   </httpErrors>
</system.webServer>

You can also use the web.config

<system.webServer>
  <httpErrors errorMode="Custom" defaultResponseMode="File" >
     <remove statusCode="404" />
     <remove statusCode="500" />
     <error statusCode="404" path="404.html" />
     <error statusCode="500" path="500.html" />
   </httpErrors>
</system.webServer>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文