2012-03-12 78 views
1

的第一段我有一個像這樣在我的web.config文件重寫規則:檢查URL沒有文件或目錄

<rule name="Public page" stopProcessing="true"> 
     <match url="^([^/]+)/([^/]+)/?$" /> 
     <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      <add input="{URL}" pattern="\.(.*)$" negate="true" /> 
     </conditions> 
     <action type="Rewrite" url="public/place/{R:2}.aspx?cname={R:1}" /> 
    </rule> 

它的作用是讓一個動態的地名客戶端類型,然後他想看到的頁面。例如,我有一個顯示公司新聞的頁面。我希望看到的新聞名爲「亭」,所以我公司類型:

http://localhost/pavilion/news/ 

和URL重寫帶我到

http://localhost/public/news.aspx?cname=pavilion 

工程。但是,我收到了很多有關如下情況的頁面錯誤:圖像的src屬性指向其他頁面的某個非現有名稱。 例如,在產品頁面(HTTP://localhost/products/showproducts.aspx)我有:

<img src='undefined'/> 

當我調試我看到它觸發規則認爲產品是{R:2}和不確定是{R:1} 所以它會試圖加載http://localhost/products/undefined/,我得到的錯誤:The file '/public/undefined.aspx' does not exist.

我想規則不火,如果該URL的第一部分是目錄或文件。 有沒有人有一個想法,我該如何做這樣的檢查?

回答

0

答案是在條件輸入字符串中使用反向引用。

<rule name="Public page" stopProcessing="true"> 
       <match url="^([^/]+)/([^/]+)/?$" /> 
       <conditions> 
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
        <add input="{URL}" pattern="\.(.*)$" negate="true" /> 
        <add input="{DOCUMENT_ROOT}\{R:1}" matchType="IsFile" negate="true" /> 
        <add input="{DOCUMENT_ROOT}\{R:1}" matchType="IsDirectory" negate="true" /> 
       </conditions> 
       <action type="Rewrite" url="public/place/{R:2}.aspx?cname={R:1}" /> 
      </rule> 

感謝Leo的答案

相關問題