Home » none » 404 Errors – Retrieving the Bad URL

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.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • ASP.NET Application_Error Detecting 404’sASP.NET Application_Error Detecting 404’s For many of you, this is going to be a "Duh!" kind of post.  But while working on this today, I found so many people asking this question and so many others giving the wrong answer, I'm […]
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it […]
  • ASP.NET Cross Domain Form SubmissionASP.NET Cross Domain Form Submission Not to be confused with cross page posting, cross domain submission allows us to post the contents of an ASP.NET form to a completely different domain. To achieve this we will need to […]
  • ASP.NET Dynamic ValidatorASP.NET Dynamic Validator One of the controls that was added to ASP.NET 3.5 in the SP1 release was the Dynamic Validator control. I completely missed it. What it does is pretty cool.  But it doesn’t […]
  • Using JSON in ASP.NETUsing JSON in ASP.NET If you've been avoiding using JSON in your ASP.NET application because you think it's hard, you're an idiot. I can say that, because I've been avoiding it for exactly that reason. I've […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • Jack

    You can also define the 404 page (aspx page) in your web.config, can in your personal 404 page (aspx page), you can use other method to get the ‘wrong’ page url.

  • Dave

    Yes, of course you can, but that’s not the question that was asked.

  • Alaa Ali

    thanks