简体   繁体   English

如何从ASP.NET MVC 2中的Application_Error重定向?

[英]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 error page) -- the same sort of browser error you get when you're offline. 完全没有代码,当上传大文件时( 不是著名的黄色ASP错误页面),我会遇到一个非常神秘的“无法显示页面”错误-与脱机时遇到的浏览器错误相同。 This strikes me as weird, and probably has something to do with this problem. 这让我感到很奇怪,并且可能与此问题有关。

I added this to my Global.asax file: 我将此添加到了Global.asax文件中:

  //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). 自定义错误在我的Web.config文件中关闭(并且必须保持这种状态)。

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; 我也尝试过将endResponse设置为true; 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. 第二个我不太确定会有什么不同,但是我已经在类似的StackOverflow问题/答案中看到了这段代码,所以我想尝试一下。

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: 如果您想自己尝试一下(看看我对非页面错误的意思),可以在VS的标准MVC应用程序中添加一些快速复制方法:

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). 然后,只需上传一个大文件(5mb),它就应该给出这个神秘的错误(无需重定向)。

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. 仅仅因为您正在回答一个问题,并不意味着您比问这个问题的人更聪明。

您可能会发现此帖子中标记为答案的项目很有帮助: 如何在ASP.NET MVC中正确处理404?

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. 如果我没记错的话,问题实际上出在IIS服务器上,而不是MVC应用程序上。 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. 但是我不知道在尝试调试MVC应用程序时VS触发的本地服务器是否可行。

In any case, I believe that I was doing something fundamentally wrong by trying to allow large file upload through an HTML form. 无论如何,我相信我试图通过HTML表单上传大文件,这在根本上是错的。 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. 相反,我实现了一个异步文件上传器,因此我不必为此担心-它提供了更好的用户体验,而且我也不必为此再费力气了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC如何在不重写URL的情况下处理Application_Error的404错误 - ASP.NET MVC how to handle 404 error from Application_Error without rewriting the URL 如何在Application_Error()中知道asp.net中的请求是ajax - How to know if the request is ajax in asp.net in Application_Error() ASP.NET MVC自定义错误处理Application_Error Global.asax? - ASP.NET MVC Custom Error Handling Application_Error Global.asax? 您是否应该在ASP.NET MVC的global.asax中使用方法Application_Error? - Should you use the method Application_Error in your global.asax in ASP.NET MVC? 在ASP.NET MVC4中,application_error哪里去了? - In ASP.NET MVC4, where has application_error gone? Asp.net mvc 覆盖基础 controller 中的 OnException 不断传播到 Application_Error - Asp.net mvc override OnException in base controller keeps propagating to Application_Error asp.net中的Application_Error与Context_Error - Application_Error vs Context_Error in asp.net 从Application_Error重定向 - redirect from Application_Error .Net Mvc:如何为Application_Error()引发错误来管理它们? - .Net Mvc: How to fire a error for Application_Error() manage them? 即使Application_Error完成,asp.net应用程序仍保持运行 - asp.net application keeps running even after Application_Error finishes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM