繁体   English   中英

如何在不更改 url 的情况下将某人重定向到错误页面

[英]How to redirect someone to the error page without changing the url

我正在使用 C# 和 asp.net 开发“错误 404”页面。 问题是,当用户键入www.example.com/asdasd时,他被重定向到我的错误 404 页面,但我希望 url 保持不变。

例如,用户键入“www.example.com/asdasd”,我想保留“www.example.com/asdasd”而不是“www.example.com/error”(这是我的错误 404 页面的名称我错误地重定向)。

这是我的 web.config 文件中的“自定义错误”

<customErrors mode="On" defaultRedirect="~/error/" redirectMode="ResponseRedirect">
    <error statusCode="404" redirect="~/error/" />
</customErrors>

这是我在 Global.asax 中的 function

 protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
            {
                Response.Redirect("~/error");
            }
        }

有什么想法或建议吗?

尝试类似:Server.Transfer("~/error")。 这会将请求服务器端“传输”到所需的页面,而无需重定向(这涉及到对错误页面的响应和另一个请求)。

Startup.cs class 的Configure方法中使用UseStatusCodePagesWithReExecute中间件:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ 
    app.UseStatusCodePagesWithReExecute("~/error");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM