在上一篇中,還有個遺留問題沒有解決,就是 ASP.NET MVC MapRoute .htm 不起作用,如果不使用 runAllManagedModulesForAllRequests="true"
的方式,該怎么解決呢?后來找了相關資料,發現了一種解決方案:
<system.webServer>
<modules runAllManagedModulesForAllRequests="false" />
<handlers>
<add name="HtmlFileHandler" path="*.htm" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
</system.webServer>
參考博文:ASP.NET MVC: Route a .html request to an MVC route
之前探討過,為什么盡量不要使用 runAllManagedModulesForAllRequests="true"
,然后我找了兩篇相關介紹:
- The art of simplicity: Optimize the performance of your web applications: Don’t use runAllManagedModulesForAllRequests="true”.
- Don't use runAllManagedModulesForAllRequests="true" when getting your MVC routing to work
摘自文中的一段描述:
This highly recommended fix can cause other problems. These problems come in the form of making all your registered HTTP modules run on every request, not just managed requests (e.g. .aspx). This means modules will run on ever .jpg .gif .css .html .pdf etc.
runAllManagedModulesForAllRequests 就像 IIS Modules 和請求的一個通道開關,如果這個開關是打開的,那么訪問此站點的所有請求都會進入 Modules 中進行處理,這其中就包含一些靜態文件的請求,這也是最常見的一種“沒必要處理”的請求,因為請求進入 Modules,那就要有相應的程序進行處理,這就造成沒必要的性能開銷,因為靜態文件只是獲取展示,完全沒必要進行 Modules 處理,小的站點無所謂,當一些很大 PV 站點也這樣做的時候,就會對 IIS 的 Modules 程序處理造成一些“壓力”,上面有篇博文總結的結論就是 waste(浪費...) 和 potential(潛在...)。
下面用 Application_BeginRequest 做一個測試,看看對 runAllManagedModulesForAllRequests 配置的不同,會有哪些請求被記錄,測試示例代碼:
protected void Application_BeginRequest(object sender,EventArgs e)
{
using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
{
_testData.WriteLine(Request.Url.ToString());
}
}
runAllManagedModulesForAllRequests="fasle",data.txt 記錄:
http://localhost:55127/
http://localhost:55127/bundles/test2?v=2Fz3B0iizV2NnnamQFrx-NbYJNTFeBJ2GM05SilbtQU1
http://localhost:55127/bundles/test1?v=MDbdFKJHBa_ctS5x4He1bMV0_RjRq8jpcIAvPpKiN6U1
runAllManagedModulesForAllRequests="true",data.txt 記錄:
http://localhost:55127/
http://localhost:55127/bundles/test2?v=2Fz3B0iizV2NnnamQFrx-NbYJNTFeBJ2GM05SilbtQU1
http://localhost:55127/bundles/test1?v=MDbdFKJHBa_ctS5x4He1bMV0_RjRq8jpcIAvPpKiN6U1
http://localhost:55127/Content/logo_small_1.gif
http://localhost:55127/Content/logo_small_4.gif
http://localhost:55127/Content/logo_small_2.gif
http://localhost:55127/Content/logo_small_3.gif
logo_small_* 圖片是我在視圖中添加的,這邊只是測試靜態圖片的請求,如果你在一個大型站點下,加上請求測試代碼,然后刷新一下頁面,你會發現無意義的請求是非常多的。可能你看上面的測試記錄,感覺似乎說明不了什么問題,但試想一下,如果一個靜態文件很多的站點,然后訪問量千萬級別的,站點包含的頁面也很多,這時候雖然一個很小的問題,但會被無限放大,最后你發現,原來只是一個配置問題。
就寫到這,要改代碼了。
文章列表