1

我的 web.config 文件中有一个重写规则,如下所示:

  <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>

它的作用是让客户输入动态地名,然后输入他想查看的页面。例如,我有一个显示公司新闻的页面。我想查看一家名为“Pavilion”的公司的新闻,所以我输入:

http://localhost/pavilion/news/ 

并且 URL 重写将我带到

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

这样可行。但是对于这样的情况,我收到了很多关于找不到页面的错误:图像的 src 属性指向另一个页面中的某个不存在的名称。例如,在产品页面 (http://localhost/products/showproducts.aspx) 我有:

<img src='undefined'/>

当我调试时,我看到它触发规则认为products是 {R:2} 并且undefined是 {R:1} 所以它尝试加载http://localhost/products/undefined/并且我收到错误:The file '/public/undefined.aspx' does not exist.

如果 url 的第一段是目录或文件,我希望规则不触发。有谁知道我该怎么做这样的检查?

4

1 回答 1

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的回答

于 2012-03-15T16:19:27.387 回答