Asp.net 如果发生maxQueryStringLength异常,如何重定向到自定义页面?

Asp.net 如果发生maxQueryStringLength异常,如何重定向到自定义页面?,asp.net,asp.net-mvc-3,Asp.net,Asp.net Mvc 3,如果请求长度超过maxQueryStringLength中指定的长度,如何替换标准错误页面,并向用户显示更友好的内容 注意:虽然作为一个HttpException,这属于一般的第400个错误,但我想分离QueryLength条件,并为这个特定错误显示一个非常特定的页面。因此,我不能使用“customErrors”部分来指示页面,而是需要以编程方式对此进行过滤。下面的问题是它不起作用 protected virtual void Application_Error(Object sende

如果请求长度超过
maxQueryStringLength
中指定的长度,如何替换标准错误页面,并向用户显示更友好的内容

注意:虽然作为一个HttpException,这属于一般的第400个错误,但我想分离QueryLength条件,并为这个特定错误显示一个非常特定的页面。因此,我不能使用“customErrors”部分来指示页面,而是需要以编程方式对此进行过滤。下面的问题是它不起作用

    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);
        }
    }

问题是Server.TransferRequest不工作。除了告诉ASP.NET要加载哪个页面之外,还有其他方法吗?

如果您能够捕获到所获取的错误类型/编号,那么您只能为此配置不同的错误/重定向页面,下面是web.config中的配置示例:

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


您可以在此处查看全文:

如果您能够捕捉到所获得的错误类型/编号,则只能为此配置不同的错误/重定向页面,这里是web.config中的配置示例:

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

您可以在此处看到全文: