网址重写不按预期工作

时间:2012-03-09 12:39:57

标签: wcf url-rewriting iis-7.5

我正在尝试重写规则,以便如果网址中没有.svc,则应重写并在网址中附加.svc

这些是我的规则:

<rewrite>   
  <rules>
    <rule name="RemoveSVC" stopProcessing="true">
      <match url="localhost/RestApp/Services/([a-zA-Z0-9]*)/(.*)$" />    
      <action type="Rewrite" url="localhost/RestApp/Services/{R:1}.svc/{R:2}" 
              appendQueryString="true" logRewrittenUrl="true" />
    </rule>    
  </rules>
</rewrite>

有人可以告诉我为什么这条规则不起作用?

1 个答案:

答案 0 :(得分:1)

我想你想要:

<rewrite>   
  <rules>
    <rule name="RemoveSVC" stopProcessing="true">
      <match url="localhost/RestApp/Services/([a-zA-Z0-9]*)/(.*)" />    
      <action type="Rewrite" url="localhost/RestApp/Services/{R:1}.svc/{R:2}" 
              appendQueryString="true" logRewrittenUrl="true" />
    </rule>    
  </rules>
</rewrite>

删除末尾的$符号。

我也摆脱了localhost/部分,因为这只适用于您的本地开发者PC,当您部署到生产环境中时,它不再是localhost

<match url="RestApp/Services/([a-zA-Z0-9]*)/(.*)" />
<action type="Rewrite" url="RestApp/Services/{R:1}.svc/{R:2}" 
        appendQueryString="true" logRewrittenUrl="true" />