c# - 404自定义重定向

标签 c# asp.net redirect http-status-code-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);
        } 

}

更新

到目前为止已经尝试过

(Response.Status == "404 Not Found")
(Response.Status == "404")
(Response.StatusCode == 404)

最佳答案

您还可以使用 web.config 的客户错误部分 - as shown here

例如在 system.web 部分,

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

关于c# - 404自定义重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7652272/

相关文章:

c# - 使用变量从 MVC 中的模型添加纬度、经度 Google Maps API

c# - 检测unity中引用的其他对象值的变化?

apache从非www重定向到www

python - django重定向ajax请求但保留 "is_ajax"

javascript - 默认的 `window.top` 是否可以作为引用无效?

c# - 在进程加载后完成操作

c# - 如何干净地构建 MS Web Api 的 REST API 端点?

c# - 是否可以在 Page_Load 事件后调用函数?

c# - 在 ASP.NET 中第一次单击时按钮 OnClick 没有触发?

asp.net - 如何摆脱这个错误 Handler "PageHandlerFactory-Integrated"has a bad module "ManagedPipelineHandler"in its module list