基于自定义HTTP标头的UrlRewrite条件

编程入门 行业动态 更新时间:2024-10-24 20:20:18
本文介绍了基于自定义HTTP标头的UrlRewrite条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在具有2个条件的UrlRewrite中配置一条规则:

I'm trying to configure a rule in UrlRewrite that has 2 conditions:

  • HTTP标头 HTTP_HOST 需要匹配某个域(api.contoso)
  • HTTP请求中必须存在一个自定义HTTP标头 x-app-version
  • HTTP header HTTP_HOST needs to match a certain domain (api.contoso)
  • A custom HTTP header x-app-version needs to be present in the HTTP request

根据此信息,我想在后端重定向到该API的其他版本.

based on this info, I'd like to redirect to a different version of the API in the back-end.

问题该规则在第一个条件下表现正常.当 HTTP_HOST 与api.contoso匹配时,它将启动.但是,它会忽略我的自定义 x-app-version 标头.

Problem The rule is behaving as expected on the first condition. It kicks in when HTTP_HOST matches api.contoso. However, it ignores my custom x-app-version header.

假设因此,我担心只能与下拉列表中有限的预定义HTTP标头集( HTTP_HOST , REMOTE_ADDRESS , REMOTE_HOST ,…)

Assumption So, I fear that a UrlRewrite condition only can be defined in combination with the limited set of predefined HTTP headers from the dropdown (HTTP_HOST, REMOTE_ADDRESS, REMOTE_HOST, …)

问题假设是正确的还是应该可行?我的配置或其他方法是否存在基于自定义定义的HTTP标头的条件的错误?

Question Is assumption correct or should this be possible whatsoever? Is there a mistake in my config or other approach to have conditions based on a custom defined HTTP header?

<rule name="ARR_test2" enabled="false"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="api.contoso" /> <add input="{x-app-version}" pattern="1" /> </conditions> <action type="Rewrite" url="FARM_apiv1/{R:0}" /> </rule>

推荐答案

好,我发现了如何在UrlRewrite条件下使用自定义HTTP标头:

Ok, I found out how to use custom HTTP headers in a UrlRewrite condition:

  • 自定义标头必须以"HTTP_"开头.
  • 用下划线代替破折号

例如:为了检索自定义标头"x-app-version",我可以使用"HTTP_x_app_version".所以我的UrlRewrite配置应该像这样

E.g.: in order to retrieve my custom header "x-app-version", I can use "HTTP_x_app_version". So my UrlRewrite config should look like this

<rule name="ARR_test2" enabled="false"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="api.contoso" /> <add input="{HTTP_x_app_version}" pattern="1" /> </conditions> <action type="Rewrite" url="FARM_apiv1/{R:0}" /> </rule>

在文档中实际上已经很清楚地提到了它: msdn.microsoft/zh-us/library/ms524602(v = vs.90).aspx

It's actually mentioned quite clear in the documentation: msdn.microsoft/en-us/library/ms524602(v=vs.90).aspx

更多推荐

基于自定义HTTP标头的UrlRewrite条件

本文发布于:2023-11-07 17:41:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   条件   HTTP   UrlRewrite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!