3

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?

RAFJR
  • 362
  • 1
  • 7
  • 22
  • I guess this might provide some useful pointers on ASP.NET error handling : http://stackoverflow.com/a/14755859/1236044 – jbl Apr 17 '13 at 09:05

2 Answers2

3

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.

Scott
  • 658
  • 6
  • 16
  • +1 Good stuff, very useful! This maintains the erroneous URL, instead of changing it to `/Error404.aspx` or whatnot. However, I am using `ASP.NET MVC 5` and `Server.Transfer` is *not* working... `Server.TransferRequest` *does* work though, but can only be called once (in the above link, the `Server.Transfer` method is called twice). – Ian Campbell Dec 24 '13 at 09:04
  • 3
    Seasons Greetings! System.Web.Routing makes this a lot easier now. Obviously, this is fundamental for MVC so you can just write a catch-all route and be done. You can also use routing in WebForms too. Check out this link: http://stackoverflow.com/questions/310580/how-can-i-make-a-catch-all-route-to-handle-404-page-not-found-queries-for-asp – Scott Dec 24 '13 at 15:26
  • Cheers @Scott! I'm actually using both techniques -- using both `Application_Error` and a catch-all route. In fact, I see that my `Application_Error` method is *not* working without the catch-all route. The improvement of using both seems to be that the URL is left in its' invalid state. Now to figure out how Google captures and displays the invalid URL... here is a link regarding this: http://blog.dmbcllc.com/404-errors-retrieving-the-bad-url/ – Ian Campbell Dec 24 '13 at 18:19
  • Thanks. @Scott. Though in my case I need to redirect it to a cshtml page. Used, this.server.TransferRequest for that matter. – jedion Oct 04 '18 at 11:05
0

you can just add: redirectMode="ResponseRewrite"

like this

<customErrors defaultRedirect="Error404.aspx" mode="RemoteOnly" 
                   redirectMode="ResponseRewrite"  >
        <error statusCode="404" redirect="Error404.aspx" />
    </customErrors>
aliadly
  • 77
  • 1
  • 11