Andy
Andy

Reputation: 2762

How to redirect to a custom page if maxQueryStringLength exception occured?

How can I replace the standard error page in the case the request length exceeds the one specified in maxQueryStringLength and show the user something more friendly?

Note: Although as an HttpException this falls into generic 400th error, I want to separate the QueryLength condition and show a very specific page for this specific error. So, I cannot use "customErrors" section to indicate a page, but rather need to filter this programmatically. The problem with the below is it doesn't work.

    protected virtual void Application_Error(Object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        if (logs != null) logs.ExceptionMsg("Application exception", this, ex);

        var httpex = ex as System.Web.HttpException;
        if (httpex != null && httpex.ErrorCode == -2147467259)
        {
            Server.ClearError();
            Server.TransferRequest(@"~/main/maxlengthexceeded", false);
        }
    }

The problem is that Server.TransferRequest does not work. Is there an alternative to how I can tell ASP.NET which page to load?

Upvotes: 1

Views: 379

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

if you are able to catch the error type/number you are getting, then you can configure a different error/redirect page only for that, here an example of configuration in the web.config:

<configuration>
  <system.web>
    <customErrors defaultRedirect="GenericError.htm"
                  mode="RemoteOnly">
      <error statusCode="500"
             redirect="InternalError.htm"/>
    </customErrors>
  </system.web>
</configuration>

you can see the full article here: Displaying a Custom Error Page

Upvotes: 1

Related Questions