3

重複の可能性:
要求の最大長がエラー ページにリダイレクトされない

最大サイズを超えるファイルをアップロードすると、ユーザーをエラーページにリダイレクトしようとします。

ファイルを 10MB に制限するために、次の行に Web.config を追加しました。

<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が完全に 2 回実行されていることを確認できServer.ClearError()ますError.aspx。代わりに、標準の見苦しい「接続がリセットされました」というエラー ページが表示されます。

このコードは、エラーが 0 による除算のような別のタイプの場合に問題なく動作しますPage_Load。ここで何が間違っているのか教えてもらえますか?

ところで。.NET 4.0、WindowsXP で Visual Web Developer 2010 Express を使用しています。VWD IIS サーバーへの組み込みのテスト。

4

2 に答える 2

1

回答:
わかりました。私が見つけた答えは次のとおりです。「通常の」例外処理では実行できません。上記のリンクの1つにあり、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");
    }
}
于 2012-10-06T18:12:39.087 に答える
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

編集:疑わしい重複を投稿しました。質問のコメントで確認してください。それはあなたのものに非常に似ています。たぶん、asp.net はこれらのタイプのエラーの処理に問題があります..

EDIT 2:また、あなたが処理しようとしているエラーはhttp 413だと思います:

<error statusCode="413" redirect="/error/upload.aspx"/>
于 2012-10-05T14:39:58.980 に答える