404 Errors – Retrieving the Bad URL
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.
Other Related Items:
Beginning ASP.NET 3.5: In C# and VB (Programmer to Programmer)This book is for anyone who wants to learn how to build rich and interactive web sites that run on the Microsoft platform. With the knowledge you gain... Read More >
Building Secure Microsoft ASP.NET Applications (Pro-Developer)This guide presents a practical, scenario-driven approach to designing and building security-enhanced ASP.NET applications for Microsoft® Windows® 2000 and version 1.1 of the Microsoft .NET Framework.
PSP Power Data CableTransfer Data & Charge Psp® Slim Or Psp® Game System From Usb; Charge & Sync Simultaneously; Dc Jack Allows Faster Charging Time










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.
Yes, of course you can, but that’s not the question that was asked.
thanks