【问题标题】:Redirect user to another page on Page_Error [duplicate]将用户重定向到 Page_Error 上的另一个页面 [重复]
【发布时间】:2012-10-07 21:11:02
【问题描述】:

可能重复:
Maximum Request Length Exceeded Not Redirect on Error Page

当他上传超过最大大小的文件时,我尝试将用户重定向到某个错误页面。

我在 Web.config 中添加了以下行以将文件限制为 10MB:

<httpRuntime maxRequestLength="10240" executionTimeout="360" />

在我的页面上有一个带有标准 ASP 文件上传控件和提交按钮的简单表单。 我还在页面级别定义了重定向(我也在 Global.asax Application_Error 处理中尝试过,但结果相同):

protected void Page_Error(object sender, EventArgs e)
{
    if (HttpContext.Current.Error is HttpException)
    {
        if ((HttpContext.Current.Error as HttpException).ErrorCode==-2147467259)
        {
            Server.ClearError();
            Response.Redirect("~/Error.aspx");
        }
    }
}

我也试过Server.Transfer() - 不工作。

当我尝试上传大于 10 MB 的文件时,我可以调试并看到来自Page_Error 的代码被完全执行了两次:即使使用Server.ClearError(),但页面没有重定向到Error.aspx。相反,会出现标准的、丑陋的“连接已重置”错误页面。

如果错误是另一种类型,例如在Page_Load 上设置的除以 0,则此代码可以正常工作。你能告诉我我在这里做错了什么吗?

顺便说一句。我将 Visual Web Developer 2010 Express 与 .NET 4.0、WindowsXP 一起使用。测试内置到 VWD IIS 服务器。

【问题讨论】:

    标签: asp.net redirect file-upload


    【解决方案1】:

    您可能想尝试在 web.config 文件中定义通用错误页面,而不是通过代码处理它:

    <customErrors defaultRedirect="/Path/to/myErrorPage.aspx" mode="On" />
    

    您还可以为每个http状态码定义特定的页面,例如:

    <customErrors defaultRedirect="/error/generic.aspx" mode="On">
        <error statusCode="404" redirect="/error/filenotfound.aspx" />
        <error statusCode="500" redirect="/error/server.html" />
    </customErrors>
    

    这是一篇不错的入门文章:http://support.microsoft.com/kb/306355

    编辑:我发布了一个疑似重复的帖子。在您问题的 cmets 中查看。它看起来和你的很相似。也许 asp.net 在处理这些类型的错误时有问题..

    编辑 2: 也是 - 我相信您要处理的错误是 http 413

    <error statusCode="413" redirect="/error/upload.aspx"/>
    

    【讨论】:

    • 是的,我忘了说我也试过了,这并没有改变我的问题......
    【解决方案2】:

    答案:
    好的,所以我找到的答案如下:它不能通过“正常”异常处理来完成。以下代码,在提到的链接之一中找到,放在 global.asax 中,是一些解决方法:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        System.Web.Configuration.HttpRuntimeSection runTime = (System.Web.Configuration.HttpRuntimeSection)System.Web.Configuration.WebConfigurationManager.GetSection("system.web/httpRuntime");
    
        //Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size
        int maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;
    
        //This code is used to check the request length of the page and if the request length is greater than
    
        //MaxRequestLength then retrun to the same page with extra query string value action=exception
    
        HttpContext context = ((HttpApplication)sender).Context;
        if (context.Request.ContentLength > maxRequestLength)
        {
            IServiceProvider provider = (IServiceProvider)context;
            HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
    
            // Check if body contains data
            if (workerRequest.HasEntityBody())
            {
                // get the total body length
                int requestLength = workerRequest.GetTotalEntityBodyLength();
    
                // Get the initial bytes loaded
                int initialBytes = 0;
    
                if (workerRequest.GetPreloadedEntityBody() != null)
                    initialBytes = workerRequest.GetPreloadedEntityBody().Length;
    
                if (!workerRequest.IsEntireEntityBodyIsPreloaded())
                {
                    byte[] buffer = new byte[512000];
    
                    // Set the received bytes to initial bytes before start reading
                    int receivedBytes = initialBytes;
    
                    while (requestLength - receivedBytes >= initialBytes)
                    {
                        // Read another set of bytes
                        initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
    
                        // Update the received bytes
                        receivedBytes += initialBytes;
                    }
                    initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
                }
            }
    
            context.Server.ClearError();  //otherwise redirect will not work as expected
            // Redirect the user
            context.Response.Redirect("~/Error.aspx");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      相关资源
      最近更新 更多