开发者

Catch IIS level error to handle in ASP.NET

I am developing an ASP.NET site with C# on IIS 7, but I hope for an answer that will apply to IIS 6 as well. Part of this site is the ability to upload up to 5 images at a time. I have a nice algorithm to resize the image that is uploaded to my optimal size and ratio.

So the only real size limitation I have is during the initial upload. I have modified my web.config to raise the packet limit from 4MB to 32MB. For the most part this takes care of my issues.

My question comes in the rare cases that a user tries to load more than my limit. I can raise the limit, but there is always a chance a user can find 5 files that are bigger. If a user selects files that are bigger, my try/catch block does not handle the error. The error is coming from IIS.

So how can I catch the error in C# code where I can make modifications to my ASP.NET interface to inform the user to select smaller files instead of them seeing a开发者_StackOverflow中文版 nasty error screen?


You can get access to the exception when the request length is exceeded. Use an HttpModule, and add a handler for the Error event.

The exception is of type: System.Web.HttpUnhandledException (with an InnerException of type: System.Web.HttpException).

To catch this exception, add this to your web.config:

<httpModules>
    <add name="ErrorHttpModule" type="ErrorHttpModule"/>
</httpModules>

And add this class to your App_Code folder:

public class ErrorHttpModule : IHttpModule
{
    private HttpApplication _context;

    public ErrorHttpModule() {
    }

    private void ErrorHandler(Object sender, EventArgs e)
    {
        Exception ex = _context.Server.GetLastError();

        //You can also call this to clear the error
        //_context.Server.ClearError();
    }

    #region IHttpModule Members

    public void Init(HttpApplication context)
    {
        _context = context;
        _context.Error += new EventHandler(ErrorHandler);
    }

    public void Dispose()
    {
    }

    #endregion
}


If you're using the traditional asp:FileUpload control, then there isn't a way to check the size of the files before. However, you can use a Flash or Silverlight approach. One option that has been suggested to me is Uploadify


I don't know for sure that this will work, but at least in IIS 7 you might try catching the Error event in an HttpModule that's configured to run for static files. From there, you could redirect to an appropriate error page.


You can catch these in Global (the global.asax.cs file). Add an Application_Error handler - you will get an HttpUnhandledException. Its InnerException will be an HttpException with the message "Maximum request length exceeded".

However, these errors are handled before your page code ever gets loaded or executed, so there is no way for your page to catch the exception or to know it ever happened. After catching this exception, you could stick a message in your Session for later display. You could also call response.Redirect from Global to display a new page, or redisplay the original with the error message from Session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜