当显示文件上传超出允许大小在ASP.NET MVC自定义错误页 [英] Display custom error page when file upload exceeds allowed size in ASP.NET MVC

查看:163
本文介绍了当显示文件上传超出允许大小在ASP.NET MVC自定义错误页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要问题是,我希望显示自定义错误页时,上传的文件超出允许大小(maxRequestLength的在web.config中)。

My main issue is that I want to display an custom error page when an uploaded file exceeds allowed size (maxRequestLength in web.config).

在大文件上传的HttpException控制器我上传的操作方法之前被抛出被调用。这是预期的。

When the big file is uploaded an HttpException is thrown before my upload action method in the controller is invoked. This is expected.

我试图捕捉一个自定义属性外,也到控制器覆盖onException的。为什么不是它可以捕获该异常无论是在属性或方法onException的?

I have tried to catch the exception in a custom attribute and also to override OnException in the controller. Why isnt it possible to catch the exception in either the attribute or the OnException method?

其可能,虽然赶在Global.asax中的Application_Error中异常但既不的Response.Redirect也不Server.Transfer的工程重定向到自定义错误页。
Server.Transfer的给出了无法处理子请求错误与Response.Redirect的给出了HTTP标头已经发出错误。

Its possible though to catch the exception in Application_Error in global.asax but neither Response.Redirect nor Server.Transfer works for redirecting to the custom error page. Server.Transfer gives the "failed to process child request" error and response.redirect gives the "Http headers already sent" error.

任何想法?

在此先感谢!

马库斯

推荐答案

在IIS7下运行,向上还有另外一个参数:

When running under IIS7 and upwards there is another parameter:

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="10485760" />
    </requestFiltering>
  </security>
</system.webServer>

默认设置略小于30 MB。

The default setting is slightly less than 30 MB.

对于大小上传的文件的maxRequestLength maxAllowedContentLength IIS7会抛出 HttpException 与HTTP code 500和消息文本超过最大请求长度。当此异常被抛出,立即IIS7杀死连接。因此,一个的HttpModule 重定向此错误只会工作,如果 HttpException 处理和清除(使用 Server.ClearError()的Application_Error>)()中的global.asax.cs。

For uploaded files with size between maxRequestLength and maxAllowedContentLength IIS7 will throw an HttpException with HTTP code 500 and message text Maximum request length exceeded. When this exception is thrown, IIS7 kills the connection immediately. So an HttpModule that redirects on this error will only work if the HttpException is handled and cleared (using Server.ClearError()) in Application_Error() in global.asax.cs.

有关大小大于 maxAllowedContentLength IIS7会显示错误code详细的错误页404和子状态code更大上传的文件 13.错误页面可以在C中找到:\\的Inetpub \\ custerr \\ EN-US \\ 404-13.htm

For uploaded files with size bigger than maxAllowedContentLength IIS7 will display a detailed error page with error code 404 and subStatusCode 13. The error page can be found in C:\inetpub\custerr\en-US\404-13.htm

有关IIS7此错误重定向我建议重定向的 httpErrors 来代替。
重定向到一个不同的动作在web.config中设置 maxAllowedContentLength 的maxRequestLength 较小的值,并添加以下内容web.config中:

For redirects on this error on IIS7 I recommend redirecting on httpErrors instead. To redirect to a different action set a smaller value for maxAllowedContentLength than maxRequestLength in web.config and also add the following to web.config:

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace"> 
    <remove statusCode="404" subStatusCode="13" /> 
    <error statusCode="404" subStatusCode="13" prefixLanguageFilePath=""
       path="http://yoursite.com/Error/UploadTooLarge" responseMode="Redirect" /> 
  </httpErrors>
</system.webServer>

这篇关于当显示文件上传超出允许大小在ASP.NET MVC自定义错误页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆