最近公司開始了一個新項目,使用的是asp.net mvc3、asp.net4.0、iis6。因為在iis6中不能支援無副檔名的路徑。所以想到了使用Url重寫。
思路是這樣的,在iis中添加.mvc的副檔名映射到C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319/aspnet_isapi.dll,這樣就可以使用/home.mvc/index這樣的路徑,然後使用url重寫
RewriteRule ^/home/index/$ /home.mvc/index/ [I,L,U]
這樣就解決了以上的問題。但是在實踐中一直顯示404錯誤。
原因如下:
ASP.NET 4.0在安裝的時候,會在IIS6註冊一個ISAPI Filter,叫做”aspnet_filter.dll”,ISAPI Filter會先於ISAPI處理常式前執行,它會在所有的的無尾碼的URL後面加一串字元“/eurl.axd/GUID”, 同時ASP.NET 4.0還會在IIS預設添加一個請求映射規則“*.axd”,映射到aspnet_isapi.dll。此時,所有的無尾碼URL加上“/eurl.axd/GUID”後都會變成帶.axd尾碼,這樣就匹配*.axd的映射規則進行ASP.NET的處理通道。在進入ASP.NET通道後,ASP.NET處理常式會刪除掉“/eurl.axd/GUID”,讓它還原到無尾碼的原始情況,並且不會對後續的請求處理帶來任何影響。此時,所有的無尾碼請求,就進入了ASP.NET的處理通道中,在預設情況下,ASP.NET4.0的全域的web.config中配置了DefaultHttpHandler來接收無尾碼的URL請求,但是我們也可以隨意更換預設處理常式(比如ASP.NET MVC處理常式)來處理無尾碼的URL請求。
ASP.NET 4.0在IIS6原生支援沒有尾碼名的URL請求
解決方案:移除aspnet_filter.dll
http://www.asp.net/learn/whitepapers/aspnet4/breaking-changes#0.1__Toc256770153
If it is not practical to remap the Web site to ASP.NET 2.0 or to change the location of a virtual directory, explicitly disable extensionless URL processing in ASP.NET 4. Use the following procedure:
- In the Windows registry, open the following node:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET\4.0.30319.0
- Create a new DWORD value named EnableExtensionlessUrls.
- Set EnableExtensionlessUrls to 0. This disables extensionless URL behavior.
- Save the registry value and close the registry editor.
- Run the iisreset command-line tool, which causes IIS to read the new registry value.