2013-02-21 124 views
3

我一直在掙扎相當一段時間如下:IIS網址重寫|如何刪除目錄和擴展名?

默認網址:

examplesite.com/folder/about.cshtml 

所需的URL:

examplesite.com/about 

基本上我要完成兩兩件事:

  • 1使用實際緊湊的代碼刪除文件擴展名。
  • 2刪除放置關於頁面的文件夾。

我發現了一些不常見的規則來實現上述所有功能,但是當我使用IIS 8.0對其進行測試時,它們大多包含大量冗餘代碼,導致網站崩潰。

所以我希望有人可以分享一個緊湊的規則,適合我的需求。或者以相同的結果分開規則。

每一個貢獻是非常讚賞:)

+2

IIRC with Razor你可以放棄'.cshtml' - 無論如何 - http://www.asp.net/web-pages/tutorials/working-with-pages/creating-readable-urls-in-aspnet-web- pages-sites – Lloyd 2013-02-21 22:10:07

回答

4

我不能肯定我完全瞭解你的需求,但這裏的東西是至少接近。它刪除第一個文件夾和文件擴展名(所以examplesite.com/folder/about.cshtml變成examplesite.com/aboutexamplesite.com/folder/help/about.cshtml變成examplesite.com/help/about)。如果你想剝離所有文件夾,然後刪除?

<rule name="Remove Directory and Extension"> 
    <match url="^(.*?)/(.*)\.cshtml$" /> 
    <action type="Rewrite" url="{R:2}" /> 
</rule> 

更新:

好吧,我想你想要的東西是那麼兩個規則的組合:

<rules> 
    <rule name="Redirect requests to friendly URLs"> 
    <match url="^(.*?)/(.*)\.cshtml$" /> 
    <action type="Redirect" url="{R:2}" /> 
    </rule> 
    <rule name="Rewrite friendly URLs to phsyical paths"> 
    <match url="^(.*)$" /> 
    <action type="Rewrite" url="folder/{R:0}.cshtml" /> 
    </rule> 
</rules> 

的第一條規則將確保所有的請求都以友好的URL。第二個採用友好的URL並將其重寫爲物理路徑,物理路徑爲folder/[FRIENDLY_PATH].cshtml

+0

它使得由重寫造成的頁面無法訪問。 – Nikita 2013-02-22 00:39:59

+0

對不起,最終目標是什麼?你希望用戶訪問'examplesite.com/about'並將其提供給'examplesite.com/folder/about.cshtml'嗎? – emjohn 2013-02-22 01:28:07

+0

是的,雖然我想要一個持久化的URL,所以'examplesite.com/folder/about.cshtml'不應該顯示在urlbar中。 – Nikita 2013-02-23 19:01:54