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");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.