Currently working on error pages for my website in ASP.NET. I'm using a web configuration file to redirect the user to an error page if the server was unable to find the requested page. Below is the customErrors tag I used in my configuration file:
<customErrors mode="On" defaultRedirect="~/ErrorPages/Error.aspx">
<error statusCode="404" redirect="~/ErrorPages/Error.aspx"/>
</customErrors>
It's currently working but my problem is that I don't want the user to see my error page in the URL as displayed below:
/MySite/ErrorPages/Error.aspx?aspxerrorpath=/MySite/ImaginaryPage.aspx
I'm expecting something like what google has: Google Error Page
Is there a way to do this without Javascript?
This article might help you out and here is a snippet from it: http://blog.dmbcllc.com/aspnet-application_error-detecting-404s/
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpException)
{
if (((HttpException)(ex)).GetHttpCode() == 404)
Server.Transfer("~/Error404.aspx");
}
}
Really the key part of this is if you want to maintain the same URL as what was requested, then Response.Redirect is NOT what you want as it goes back to the client to issue a second request thus changing the URL. You want to remain on the server so the appropriate call would be Server.Transfer. This particular event handler goes into your Global.asax.cs file and the method will get raised up for any 404s that are associated within the context of your application.
Unfortunately, it will skip past your customErrors configuration section and rely on a more programmatic approach however the code is fairly simple from a maintenance standpoint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With