Question

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/

Était-ce utile?

La 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

Autres conseils

Try putting the following code in your global.asax file:

void Application_Error(object sender, EventArgs e)
{
    string host = Context.Request.Url.Host;
    string redirectUrl = string.Format("~/Error.html?aspxerrorpath={0}&aspxerrorhost={1}", Server.UrlEncode(Context.Request.Path), Server.UrlEncode(host));
    Response.Redirect(redirectUrl);
}

This is essentially creating your own error handler to redirect to the error page, which is more flexible as you can add whatever query parameters you wish to the url.

To use this method, you won't be able to configure the redirect in your web.config file the normal way. You could, however, use the appSettings section of the file, and then read the values like this:

Web.config file

<appSettings>
  <add key="redirectPath" value="valueGoesHere"/>
</appSettings>

C# Error Handler

string redirectPath = System.Configuration.ConfigurationManager.AppSettings["redirectPath"];

EDIT:

Also, if you need to redirect to different pages depending on the http error code, use the following code in the error handler:

HttpException exception = Server.GetLastError() as HttpException;
int errorCode = -1;

if(exception != null)
{
    errorCode = exception.GetHttpCode();
}

switch (errorCode)
{
    case 404:
        // Redirect here
        break;
    default:
        // Redirect here
        break;
}

Maybe using a custom filter attribute is the solution (implementing the OnActionExecuted to check if an error has occurred and modifying the result)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top