[英]How to redirect someone to the error page without changing the url
I'm developing a 'error 404' page using C# and asp.net.我正在使用 C# 和 asp.net 开发“错误 404”页面。 The problem is, when the user types www.example.com/asdasd he is redirected to my error 404 page, but I wanted the url to stay the same.问题是,当用户键入www.example.com/asdasd时,他被重定向到我的错误 404 页面,但我希望 url 保持不变。
For example, the user types 'www.example.com/asdasd', I wanted to stay 'www.example.com/asdasd' not 'www.example.com/error' (which is the name of my error 404 page that I'm wrongly redirecting).例如,用户键入“www.example.com/asdasd”,我想保留“www.example.com/asdasd”而不是“www.example.com/error”(这是我的错误 404 页面的名称我错误地重定向)。
This is my 'Custom Errors' inside my web.config file这是我的 web.config 文件中的“自定义错误”
<customErrors mode="On" defaultRedirect="~/error/" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/error/" />
</customErrors>
This is my function inside Global.asax这是我在 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");
}
}
Any ideas or suggestions?有什么想法或建议吗?
Try something like: Server.Transfer("~/error").尝试类似:Server.Transfer("~/error")。 This 'transfers' the request server side to the desired page without a redirect (which involves a response and another request to the error page).这会将请求服务器端“传输”到所需的页面,而无需重定向(这涉及到对错误页面的响应和另一个请求)。
Inside Configure
method in Startup.cs
class use UseStatusCodePagesWithReExecute
middleware:在Startup.cs
class 的Configure
方法中使用UseStatusCodePagesWithReExecute
中间件:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStatusCodePagesWithReExecute("~/error");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.