I'm developing a 'error 404' page using C# and asp.net. 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.
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).
This is my 'Custom Errors' inside my web.config file
<customErrors mode="On" defaultRedirect="~/error/" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/error/" />
</customErrors>
This is my function inside Global.asax
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"). 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:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStatusCodePagesWithReExecute("~/error");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.