【问题标题】:Get specified url part in policy with Azure APIM Policy使用 Azure APIM Policy 获取策略中指定的 url 部分
【发布时间】:2019-07-18 16:22:32
【问题描述】:

是否可以在策略内获取以下指定的 url 部分:

<policies>
        <inbound>
            <base />    
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
            <find-and-replace from="https://thirdparty/certs" 
                 to="@(specified url part)" />        
        </outbound>
        <on-error>
            <base />
        </on-error>
    </policies>

请注意,整个 url 可能包含版本控制和前缀。

【问题讨论】:

    标签: azure azure-api-management


    【解决方案1】:

    整个 URI(假设 https://contoso.com/myapi/.wellknown/openid-config)由以下部分组成:

    1. 方案:context.Request.OriginalUrl.Scheme
    2. 主持人:context.Request.OriginalUrl.Host
    3. 可选端口:context.Request.OriginalUrl.Port
    4. API 后缀:context.Api.Path
    5. 操作路径:其余部分

    所以URL的操作部分可以通过接近于:context.Request.OriginalUrl.Path.Trim('/').Substring(context.Api.Path.Trim('/').Length)的东西来计算

    【讨论】:

    • context.Api 对我来说是空的。是因为它需要发布 API 吗?有其他选择吗?
    • 只有在没有为请求识别 API 时才会发生这种情况,此类调用通常会导致 404。您确定您正在进行正确的测试调用吗?如果您删除此政策,您会得到适当的回应吗?
    • 是的,它可以在没有策略的情况下工作。但是context.Api在添加的时候是null。
    【解决方案2】:

    策略表达式示例,当在入站路径中使用时,可用于根据已发布 API 中作为查询参数包含的版本信息重定向传入请求。

    <choose>
        <when condition="@(context.Request.Url.Query.GetValueOrDefault("version") == "2014-03")">
          <set-backend-service base-url="http://contoso.com/api/1.6/" />
        </when>
        <otherwise>
          <set-backend-service base-url="http://contoso.com/api/2.0/"
        </otherwise>
      </choose>

    https://azure.microsoft.com/en-us/blog/policy-expressions-in-azure-api-management/了解更多详情

    【讨论】:

    • 感谢您的信息。但这不是答案,因为我正在寻找开篇文章中提到的指定 url 部分。我知道您帖子中提供的网址,其中没有关于我的问题的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2019-06-01
    • 2017-08-28
    • 2020-10-29
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多