Search code examples
c#asp.nethttp-status-code-404custom-error-handling

How to add additional query string parameters when 404 redirect occurs


We are currently using a custom error page for 404 errors for our Web App. When the redirect occurs aspxerrorpath contains the path the user was trying to access. Is it possible to catch the error in Application_Error within Global.asax and append a new parameter to the query string before the redirect occurs? I'd like to append the host the user was requesting.

My understanding is that IIS handles the 404 redirect and appending the aspxerrorpath param to the 404 redirect, I'd like to intercept this before it occurs and append another param to the querystring.

More Info: We have multiple sites within a CMS so the requested url will not always be www.example.com but the 404 redirect will always be to www.example.com.

Currently: user requests www.example.com/fakepage.aspx and is redirected to...

www.example.com/404.aspx?aspxerrorpath=/fakepage.aspx

What I want: user requests www.example.com/fakepage.aspx and is redirected to...

www.example.com/404.aspx?aspxerrorpath=/fakepage.aspx&aspxerrorhost=example.com

SOLUTION: I ended up implementing a custom HttpModule (see the answer below). I just wanted to pass along an excellent article explaining how to do this. http://helephant.com/2009/02/11/improving-the-way-aspnet-handles-404-requests/


Solution

  • In order to trap the error and append the querysting you will need to write you own HttpHandler by inheriting the IHttpHandler interface and add the custom code in ProcessRequest method

    Yes this will be for all request but can trap the one your after

    Please see link for how to do : MSDN