views:

136

answers:

3

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).

A: 

Same problem here. And please don't tell me you can't do a redirect, because the same exact code is working on a different site on the same server. It has to be something with my site's configuration.

Joe
A: 

you might find the item marked as the answer in this posting helpful: http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/2577095

Jonathan Bates
+1  A: 

I was able to fix my problem. The error handling was also sending out an email detailing the error. That was throwing another exception. But the exception is not allowed to bubble up so I was unable to see it.

In short, make sure that nothing is throwing another exception and all should work fine.

And contrary to what the other poster posted, you don't have to rewrite your entire error handling block...God I hate when people do that. "Oh, I know the answer to your question. Just redo it all following this example!". How about you read the code and try to help debug it. If you can't then just don't respond. Just because you're responding to a question, that doesn't mean you're smarter than the person asking it.

Joe