2

I'm trying to figure out how to handle Maximum request length exceeded exception. If that exception is being thrown, I must invoke other controller's action. The problem is, the UploadError action is not being invoked - why ?

Here's the code snippet:

<httpRuntime maxRequestLength="1024" /> 
...
<requestLimits maxAllowedContentLength="1048576"/>


    const int TimedOutExceptionCode = -2147467259;
    public bool IsMaxRequestExceededException(Exception e)
    {
        // unhandled errors = caught at global.ascx level
        // http exception = caught at page level

        Exception main;
        var unhandled = e as HttpUnhandledException;

        if (unhandled != null && unhandled.ErrorCode == TimedOutExceptionCode)
        {
            main = unhandled.InnerException;
        }
        else
        {
            main = e;
        }


        var http = main as HttpException;

        if (http != null && http.ErrorCode == TimedOutExceptionCode)
        {
            // hack: no real method of identifying if the error is max request exceeded as 
            // it is treated as a timeout exception
            if (http.StackTrace.Contains("GetEntireRawContent"))
            {
                // MAX REQUEST HAS BEEN EXCEEDED
                return true;
            }
        }

        return false;
    }


    protected void Application_Error()
    {
        var exception = Server.GetLastError();
        var routeData = new RouteData();

        if (exception.GetType() == typeof(UploadException) || IsMaxRequestExceededException(exception))
        {
            exception = new UploadException(exception.Message);
            Response.Clear();
            Server.ClearError();
            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = "UploadError";
            routeData.Values["exception"] = exception;

            IController errorsController = new ErrorController();
            var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
            errorsController.Execute(rc);
            return;
        }
    }

public class ErrorController : Controller
{
    public ActionResult UploadError(UploadException exception)
    {
        return Content(exception.Message, "text/plain");
    }
}
Tony
  • 12,405
  • 36
  • 126
  • 226

1 Answers1

0

Look at this anwer:

https://stackoverflow.com/a/679427/685319

There is the problem explained.

Community
  • 1
  • 1
denyo85
  • 69
  • 5