【问题标题】:Policy rewrite-uri To Append Context Variable in Azure APIM策略 rewrite-uri 在 Azure APIM 中附加上下文变量
【发布时间】:2018-10-29 03:28:53
【问题描述】:

在策略重写期间简单附加到上下文变量(例如 context.Variables["accountKey"])的 url 的方法是什么?

最终结果应该是/accounts/232

我之前设置成功了

set-variable (0.003 ms)
{
    "message": "Context variable was successfully set.",
    "name": "accountKey",
    "value": "232"
}

尝试过的事情之一:

<policies>
    <inbound>
        <base />
        <rewrite-uri template="/accounts/{accountKey}" />
    </inbound>

但我得到了这个错误

> Error Receive
>     rewrite-uri (0.260 ms) {
>     "messages": [
>         null,
>         "Variable accountKey has no value.",
>         "Variable accountKey has no value."
>     ] }

【问题讨论】:

    标签: azure url-rewriting policy azure-api-management


    【解决方案1】:

    在策略中配置入站规则如下:

    <inbound>
        <base />
        <set-variable name="accountKey" value="232" />
        <rewrite-uri template="@{
            return "/account/" + context.Variables.GetValueOrDefault<string>("accountKey");
        }"/>
    </inbound>
    
    rewrite-uri 中的

    {} 用于原始请求 URL 中的查询字符串参数。

    Rewrite URL - API Management transformation policies 的 Microsoft 文档中查找有关 rewrite-uri 部分的更多详细信息。

    【讨论】:

      【解决方案2】:

      我最终使用了这个调用作为替代方法:

      &lt;rewrite-uri template="@($"/account/{(string)context.Variables["accountKey"]}/")" /&gt;

      【讨论】:

        【解决方案3】:

        就我而言,我的网址是

        https://test.com/send/
        

        我需要从上下文变量名称中添加查询字符串 (name = myName)

        https://test.com/send/myName
        

        这对我有用:

        <rewrite-uri template="@($"{(string)context.Variables["name"]}")" />
        

        【讨论】:

          猜你喜欢
          • 2019-06-16
          • 1970-01-01
          • 2020-10-29
          • 2017-08-28
          • 2018-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-02
          相关资源
          最近更新 更多