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

时间:2011-09-17 12:31:05

标签: asp.net asp.net-mvc-3

如果请求长度超过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要加载哪个页面?

1 个答案:

答案 0 :(得分:1)

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

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

您可以在此处查看完整文章:Displaying a Custom Error Page

相关问题