我有一個aspx頁面,允許用戶上傳文件,並且我想限制最大文件上傳大小爲10MB。 IIS7,.NET 3.5。我有以下的在我的web.config文件中配置:我在哪裏可以捕獲並處理IIS7中超出的maxAllowedContentLength?
<location path="foo.aspx">
<system.web>
<!-- maxRequestLength: kbytes, executionTimeout:seconds -->
<httpRuntime maxRequestLength="10240" executionTimeout="120" />
<authorization>
<allow roles="customRole"/>
<!-- Deny everyone else -->
<deny users="*"/>
</authorization>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!-- maxAllowedContentLength: bytes -->
<requestLimits maxAllowedContentLength="10240000"/>
</requestFiltering>
</security>
<handlers accessPolicy="Read, Script">
<add name="foo" path="foo.aspx" verb="POST"
type="System.Web.UI.PageHandlerFactory"
preCondition="integratedMode" />
</handlers>
</system.webServer>
</location>
我有一個自定義錯誤處理,實現IHttpModule
模塊。我發現當超過maxRequestLength
時,HttpApplication.Error
確實得到提升。但是,當我玩maxAllowedContentLength
時,HttpApplication.Error
事件沒有被引發,用戶被重定向到404.13頁面。我附加了Visual Studio,第一次機會異常處於開啓狀態,沒有任何內容被拋出。
我的第一個想法是在早期事件中檢查標題內容的長度 - 是否有建議/最佳做法,我在哪裏做這件事? PostLogRequest? EndRequest?
請注意,如果託管管道模式設置爲Integrated,sunflowerpower的答案纔有效。至少這就是我經歷過的原因 – Marvin 2010-10-27 12:34:50
我注意到這個解決方案無法正確處理身份驗證。當我調用Server.Transfer時,目標頁面的身份驗證例程失敗,因爲HttpContext.Current.User屬性爲NULL。它似乎不是由清除標題引起的,因爲刪除該行仍然會導致問題。有沒有辦法解決?否則這不是一個解決方案。 – Triynko 2011-04-14 22:00:54