When uploading a file in my MVC site, I'm looking to handle upload failures due to the user exceeding maxRequestLength more gracefully than for example showing a generic custom error page. I would prefer to display the same page they're trying to post to, but with a message informing them that their file was too large... similar to what they might get on a validation error.
I'm starting with the idea from this question:
Catching "Maximum request length exceeded"
BUT what I want to do is instead of Transferring to an error page (as they do in that question), I want to hand processing off to the original controller but with an error added to ModelState indicating the issue. Here's some code, with the comments indicating where and what I would like to do. See the question above for definition of IsMaxRequestExceededEexception, which is a bit of a hack, but I haven't found much better.
The line I've commented out returns the user to the right page, but of course they lose any changes they may have made and I don't want to use Redirect here...
if (IsMaxRequestExceededException(Server.GetLastError()))
{
Server.ClearError();
//((HttpApplication) sender).Context.Response.Redirect(Request.Url.LocalPath + "?maxLengthExceeded=true");
// TODO: Replace above line - instead tranfer processing to appropriate controlller with context intact, etc
// but with an extra error added to ModelState.
}
Just looking for ideas rather than a full solution; is what I'm trying to do is even possible?