2011-12-14 41 views
0

我需要在運行IIS7的ASP.NET應用程序的web.config中設置一些301永久性重定向。IIS7和永久重定向使用web.config中的位置標記

<configuration> 
    <location path="services.htm"> 
    <system.webServer> 
     <httpRedirect enabled="true" destination="default.aspx" httpResponseStatus="Permanent" /> 
    </system.webServer> 
    </location> 
    <location path="products.htm"> 
    <system.webServer> 
     <httpRedirect enabled="true" destination="default.aspx" httpResponseStatus="Permanent" /> 
    </system.webServer> 
    </location> 
</configuration> 

所有我從重定向到主頁重定向的頁面 - 有沒有這樣做,因爲我有超過10頁我需要重定向到Default.aspx的的更快和/或更簡單的方法?我可以爲10個頁面中的每一頁指定一個位置部分,但希望採用更簡潔的方法。

回答

0

您可以設置.htm由ASP.NET庫(與.aspx默認相同)處理,然後查看global.ascx中Application_BeginRequest中的請求,並在適當的時候重定向。

它比Web.config中的一個或兩個重定向更復雜一些,但在一定量之後它變得更容易。

+0

如果文件系統上不存在.htm/.aspx文件,global.ascx中的Application_BeginRequest將不會被觸發。 – amateur 2011-12-14 23:48:40

0

在web.config中逐個輸入頁面的替代方法是創建一個HttpModule。在IIS7 +處於集成模式下,默認HttpModules將運行所有內容,包括靜態文件。

要做代碼重定向,請在早期事件中調用HttpResponse.RedirectPermanent(string url, bool endResponse)。爲了獲得最佳性能,請設置endResponse = true。