Catch IIS-Level Error for Processing in ASP.NET

I am developing an ASP.NET site with C # in IIS 7, but I hope for an answer that will apply to IIS 6. Part of this site is the ability to upload up to 5 images at a time. I have a good algorithm for resizing an image that loads to the optimal size and ratio.

So, the only size limit I have is during bootstrap. I modified my web.config to increase the packet limit from 4 MB to 32 MB. For the most part, this takes care of my problems.

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

So, how can I catch an error in C # code, where can I make changes to my ASP.NET interface to inform the user about choosing small files instead of seeing an unpleasant error screen?

+3
source share
4 answers

You can access the exception if the request length is exceeded. Use the HttpModule and add a handler for the Error event.

: System.Web.HttpUnhandledException ( InnerException : System.Web.HttpException).

, web.config:

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

App_Code:

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
}
+6

asp: FileUpload, . Flash Silverlight. , , - Uploadify

+2

, , , , IIS 7 Error HttpModule, . .

+1

Global ( global.asax.cs). Application_Error - HttpUnhandledException. InnerException HttpException " ".

However, these errors are handled before your page code is ever loaded or executed, so there is no way for your page to catch an exception or to know that it ever happened. After detecting this exception, you can insert a message into your session for later display. You can also call response.Redirect from Global to display a new page, or re-display the original with an error message from the session.

+1
source

Source: https://habr.com/ru/post/1727059/


All Articles