Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to create a 404 handler that can record the *exact* URL that
caused the 404 to occur.
When I turn on custom error handling in the web.config, ASP.NET successful
redirects the 404 to my custom page (404.aspx), but the aspxerrorpath
querystring variable only shows what page the request went to, not the
querystring variables passed to that page.
I need the *complete* URL that causes the error, including the querystring
arguments.
How can I obtain this?
S.Srikanth.

i am having following code in global.asax
void Application_Error(object sender, EventArgs e) 
    {
        //get reference to the source of the exception chain
        Exception ex = Server.GetLastError().GetBaseException();
        String url = String.Empty;
        if (Request != null && Request.Url != null)
        {
            url = Request.Url.ToString();
        }
        
        //log the details of the exception and page state to the
        Logger.Fatal(string.Format("Unhandled appplication exception occured." +
          "\nMESSAGE: {0}" +
          "\nSOURCE: {1}" +
          "\nSTACKTRACE: {2}" +
          "\nURL: {3}",
          ex.Message,
          ex.Source,
          ex.StackTrace,
          url));
        
        HttpException httpEx = ex as HttpException;
        if (httpEx != null && httpEx.GetHttpCode() == 404)
        {
            Response.Redirect(AuctionsConstants.Error404Page);
        }
        else
        {
            // redirect to the global error page
            Response.Redirect(AuctionsConstants.ErrorPage);
        }     
        
    }


web.config
------------
<customerrors mode="on" defaultredirect="~/errorpages/Error.aspx?">
<error statuscode="404" redirect="~/errorpages/error404.aspx" />
</customerrors>
Posted
Updated 20-Apr-11 22:13pm
v2

1 solution

Hi you can get the query string from the HttpRequest object of the context. The context can be accessed anywhere in the application like this

C#
HttpContext context=HttpContext.Current;
HttpRequest req = context.Request;
string param1 = req.QueryString["testparam"];


I won't recommend logging page specific context parameters in the global Application Error handler. Instead have this logic in each page by handling the Page.Error event.
 
Share this answer
 
Comments
Sandeep Mewara 27-Apr-11 12:09pm    
Right it is. 5!
Albin Abel 27-Apr-11 12:11pm    
Thanks Sandeep

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900