I want to show a custom error page if a user tries to upload a file larger than the maximum request length.
With no code at all, I get a very mysterious "The page cannot be displayed" error when uploading a large file (not the famous yellow ASP error page) -- the same sort of browser error you get when you're offline. This strikes me as weird, and probably has something to do with this problem.
I added this to my Global.asax file:
//simplification
public void Application_Error(object sender, EventArgs e)
{
Response.Redirect("http://www.google.com", false); //This IS getting hit
}
Custom errors are off in my Web.config file (and must stay that way).
Seems pretty simple, right? But for whatever reason, that redirect just isn't doing anything. It is getting hit. It is executing that line. I have tried it with endResponse
set to true as well; no difference.
I've tried with the following two lines before the redirect:
Response.Clear();
Server.ClearError();
The first I assume would address a redirect occurring after headers had been sent (which is not the case); the second I'm not really sure what difference that would make, but I have seen this code in similar StackOverflow questions/answers, so I thought I'd try it.
So -- is there something peculiar about this particular error that makes redirects impossible?
If you'd like to try it for yourself (and see what I mean about the weird error non-page), here's some quick copypasta to add to VS's standard MVC app:
Views -> Home -> Index.aspx
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<form enctype="multipart/form-data" method="post" action="<%= Url.Action("Upload") %>">
<input type="file" name="required-to-post" />
<input type="submit" value="Upload" />
</form>
</asp:Content>
Controllers -> HomeController.cs
public ActionResult Upload()
{
return View("Index");
}
Global.asax
protected void Application_Error()
{
Response.Redirect("http://www.google.com", false);
}
Then just upload a large file (5mb) and it ought give this mysterious error (without redirecting).