404 Errors – Retrieving the Bad URL

arct-071 A few weeks ago, I wrote a post describing how to detect that a 404 error had occurred in your error handler rather than just detecting that any error had occurred.

ASP.NET Application_Error – Detecting 404′s

This past weekend Alaa Ali asked, “So how do you retrieve the wrong requested URL?”

It’s actually pretty easy.

At the point this error occurs, you are requesting the bad URL.  Unlike how we use to do it with ASP, which redirected to the error page where we got the bad URL out of the parameter that was passed to the 404 handler, all we need to do is retrieve the currently requested page name from the URL property of the Request object.

To get the path and query string together use:

string badRequest = Request.Url.PathAndQuery;

To get just the path and filename use:

string badRequest = Request.Url.LocalPath;

Once you have the information, you can then do something with it, like try to figure out what file they were trying to get to and sending them to that instead.

Related Post

3 Responses to “404 Errors – Retrieving the Bad URL”

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear