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.
- ASP.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 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 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 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.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