服务版本控制场景的 IIS7 URL 重写规则

发布于 2025-01-01 23:13:58 字数 944 浏览 1 评论 0 原文

要对 RESTful 服务进行版本控制,我想使用 URL 重写模块执行以下操作;

如果 Accept 标头(HTTP_ACCEPT?)是 application,则将 https://server/service/ 映射到 server/service/v1/ /vnd.mycompany.service+xml

对于该服务的下一个版本,我想增加媒体类型的版本号,并部署 v2,以便我可以执行以下映射:

Map https://server/service/ 到 server/service/v2/ 如果 Accept 标头(HTTP_ACCEPT?)是 application/vnd.mycompany.service-2+xml

我已经尝试过遵循规则,但我无法让它工作:

<rules>
   <rule name="Route request to correct service version" patternSyntax="Wildcard">
      <match url="*service*" />
         <conditions>
            <add input="{HTTP_ACCEPT}" pattern="application/vnd.mycompany.*" />
         </conditions>
      <action type="Rewrite" url="Service/v1/{R:2}" logRewrittenUrl="true" />
   </rule>
</rules>

任何帮助表示感谢!

--larsw

To version a RESTful service, I would like to do the following with the URL Rewrite Module;

Map https://server/service/ to server/service/v1/ if the Accept header (HTTP_ACCEPT?) is application/vnd.mycompany.service+xml

For the next version of the service, I'd like to bump the version number of the media type, and deploy a v2, so that I can do the following mapping:

Map https://server/service/ to server/service/v2/ if the Accept header (HTTP_ACCEPT?) is application/vnd.mycompany.service-2+xml

I've tried the following rule, but I can't get it to work:

<rules>
   <rule name="Route request to correct service version" patternSyntax="Wildcard">
      <match url="*service*" />
         <conditions>
            <add input="{HTTP_ACCEPT}" pattern="application/vnd.mycompany.*" />
         </conditions>
      <action type="Rewrite" url="Service/v1/{R:2}" logRewrittenUrl="true" />
   </rule>
</rules>

Any help appreciated!

--larsw

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的味道 2025-01-08 23:13:58

由于不熟悉 URL 重写机制,我无法直接帮助您,但是,如果您要使用 Web API 托管服务,那么 HttpMessageHandler 将能够根据接受标头进行这种 URL 转换。

Being unfamiliar with the URL rewriting mechanism, I can't help you directly, however, if you were to host your service with Web API, then a HttpMessageHandler would be able to do this kind of URL conversion based on the accept header.

烟酉 2025-01-08 23:13:58

解决了。

a) 不知何故,RedirectModule 未在 applicationHost.config 中连接。
b) 以下配置有效:

<rule name="Route request to correct service version" patternSyntax="Wildcard">                           
  <match url="*service*" />                                                                  
       <conditions logicalGrouping="MatchAny" trackAllCaptures="true">                                    
          <add input="{HTTP_ACCEPT}" pattern="*application/vnd.mycompany.*" />                             
       </conditions>                                                                                      
       <action type="Rewrite" url="service/v1{R:2}" logRewrittenUrl="true" />                  
</rule> 

c) 我在 {R:2} 变量之前有一个额外的斜杠,导致重写的 url 具有双斜杠。

--larsw

Solved it.

a) Somehow the RedirectModule wasn't wired up in applicationHost.config.
b) The following configuration worked:

<rule name="Route request to correct service version" patternSyntax="Wildcard">                           
  <match url="*service*" />                                                                  
       <conditions logicalGrouping="MatchAny" trackAllCaptures="true">                                    
          <add input="{HTTP_ACCEPT}" pattern="*application/vnd.mycompany.*" />                             
       </conditions>                                                                                      
       <action type="Rewrite" url="service/v1{R:2}" logRewrittenUrl="true" />                  
</rule> 

c) I had an extra slash before the {R:2} variable that caused the rewritten url to have a double slash.

--larsw

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文