我的主要问题是,当上传的文件超过允许的大小(web.config 中的 maxRequestLength)时,我想显示自定义错误页面。
上传大文件时,在调用控制器中的上传操作方法之前会引发 HttpException。这是预料之中的。
我尝试捕获自定义属性中的异常,并覆盖控制器中的 OnException 。为什么无法在属性或 OnException 方法中捕获异常?
尽管可以捕获 global.asax 中的 Application_Error 中的异常,但 Response.Redirect 和 Server.Transfer 都无法重定向到自定义错误页面。
Server.Transfer 给出“无法处理子请求”错误,response.redirect 给出“Http 标头已发送”错误。
有任何想法吗?
提前致谢!
Marcus
当在 IIS7 及以上版本下运行时,还有另一个参数:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="10485760" />
</requestFiltering>
</security>
</system.webServer>
默认设置略小于 30 MB。
对于大小介于maxRequestLength
and maxAllowedContentLength
IIS7 会抛出一个HttpException
带有 HTTP 代码 500 和消息文本Maximum request length exceeded
。当抛出此异常时,IIS7立即终止连接。所以一个HttpModule
仅当以下情况时,重定向此错误才会起作用HttpException
被处理和清除(使用Server.ClearError()
) in Application_Error()
在 global.asax.cs 中。
对于上传的文件大小大于maxAllowedContentLength
IIS7将显示详细的错误页面,错误代码为404,subStatusCode
13. 错误页面可以在C:\inetpub\custerr\en-US\404-13.htm中找到
对于 IIS7 上此错误的重定向,我建议重定向httpErrors
反而。
要重定向到不同的操作,请设置较小的值maxAllowedContentLength
than maxRequestLength
在 web.config 中并将以下内容添加到 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)