问题描述
我的主要问题是,当上传的文件超过允许的大小(web.config 中的 maxRequestLength)时,我想显示一个自定义错误页面.
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 headers already sent"错误.
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 将抛出带有 HTTP 代码 500 和消息文本 Maximum request 的
.当抛出此异常时,IIS7 会立即终止连接.因此,仅当 HttpException
超出长度HttpException
在 中被处理和清除(使用
在 global.asax.cs 中.Server.ClearError()
)时,重定向此错误的 HttpModule
才会起作用>Application_Error()
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 将显示详细的错误页面,错误代码为 404 和 subStatusCode
13.错误页面可以在 C:inetpubcusterren-US404-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:inetpubcusterren-US404-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 中允许的大小时显示自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!