开发者

How to redirect from Application_Error in ASP.NET MVC 2?

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 er开发者_高级运维ror 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).


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.


you might find the item marked as the answer in this posting helpful: How can I properly handle 404 in ASP.NET MVC?


This is a pretty old question, and one that I solved a while ago, although I never posted an answer here.

If I recall correctly, the problem was actually with the IIS server, not the MVC app. The server itself had a maximum request length, and things behaved very strangely when this rule was violated.

By changing the maximum server request size, I believe that the problem disappeared. But I don't know if this is possible in the local server that VS fires up when you try to debug an MVC app.

In any case, I believe that I was doing something fundamentally wrong by trying to allow large file upload through an HTML form. I instead implemented an asynchronous file uploader so that I don't have to worry about this -- it provides a much better user epxerience, and I don't have to scratch my head over this any more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜