2011年12月8日木曜日

[ASP]maxrequestlengthのエラー処理

ASPの場合、ファイルのアップロードの制限をしたいという時は、web.configでmaxrequestlengthを設定しますが、そのままだとmaxrequestlengthを超えた場合に”ページを表示できません”画面が表示されてしまいます。そのままだと、どういったエラーが出ているのかユーザに通知できないのでサポートも面倒くさいです。そこで、カスタムエラーを設定すればいいのかと思っていたら、ちょっと違うようです。

詳しくは、
http://www.developer.com/db/article.php/10920_3426051_2
にありますが、どうも日本語では見つからなかったので覚書です。


Sub Application_Error(ByVal sender As Object, _
 ByVal e As EventArgs)
    ' Fires when an error occurs
    ' Check to see whether we came 
    ' from the upload form
    If Path.GetFileName(Request.Path) = _
     "UploadForm.aspx" Then
        ' Get the error details
        Dim appException As System.Exception = _
         Server.GetLastError()
        Dim checkException As HttpException = _
         CType(appException, HttpException)
        ' Verify the expected error
        If checkException.GetHttpCode = 400 And _
         checkException.ErrorCode = -2147467259 Then
            ' Error 400 = bad request, user 
            ' tried to upload a file that's too large
            Session("ImageTooLarge") = True
            Server.ClearError()
            ' Go to the original target page
            Response.Redirect("UploadForm.aspx")
        End If
    End If
    ' For other errors, just accept the default processing
End Sub

0 件のコメント: