控制流

使用 choose 策略根据布尔表达式的计算结果有条件地应用策略语句。 使用类似于编程语言中的 if-then-else 或 switch 构造的控制流的策略。

注意

按照策略声明中提供的顺序设置策略的元素和子元素。 详细了解如何设置或编辑 API 管理策略

策略语句

<choose>
    <when condition="Boolean expression | Boolean constant">
        <!— one or more policy statements to be applied if the above condition is true  -->
    </when>
    <when condition="Boolean expression | Boolean constant">
        <!— one or more policy statements to be applied if the above condition is true  -->
    </when>
    <otherwise>
        <!— one or more policy statements to be applied if none of the above conditions are true  -->
    </otherwise>
</choose>

choose 策略必须至少包含一个 <when/> 元素。 <otherwise/> 元素是可选的。 <when/> 元素中的条件根据其在策略中的出现顺序求值。 将应用条件属性等于 true 的第一个 <when/> 元素中括住的策略语句。 在所有 <when/> 元素条件属性为 false 的情况下,将应用 <otherwise/> 元素中括住的策略(如果存在)。

元素

元素 说明 必需
when 一个或多个指定 choose 策略的 ififelse 部分的元素。 如果指定了多个 when 元素,它们将按顺序计算。 一旦 when 元素的 condition 的求值结果为 true,不再对 when 条件求值。
otherwise 在没有任何 when 条件的计算结果为 true 的情况下进行计算的策略片段。

when 属性

属性 说明 必需
condition 对包含 when 的策略语句求值时需求值的布尔表达式或布尔常量。

使用情况

示例

基于用户代理修改请求和响应

以下示例演示 set-variable 策略和两项控制流策略。

此 set-variable 策略位于入站节,用于创建 isMobile 布尔isMobile变量,该变量在 User-Agent 请求标头包含文本 iPadiPhone 的情况下设置为 true。

第一项控制流策略也位于入站节,并会根据 isMobile 上下文变量的值有条件地应用两项设置查询字符串参数策略之一。

第二项控制流策略位于出站节,并会在 isMobile 设置为 true 时有条件地应用将 XML 转换为 JSON 策略。

<policies>
    <inbound>
        <set-variable name="isMobile" value="@(context.Request.Headers.GetValueOrDefault("User-Agent","").Contains("iPad") || context.Request.Headers.GetValueOrDefault("User-Agent","").Contains("iPhone"))" />
        <base />
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<bool>("isMobile"))">
                <set-query-parameter name="mobile" exists-action="override">
                    <value>true</value>
                </set-query-parameter>
            </when>
            <otherwise>
                <set-query-parameter name="mobile" exists-action="override">
                    <value>false</value>
                </set-query-parameter>
            </otherwise>
        </choose>
    </inbound>
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<bool>("isMobile"))">
                <xml-to-json kind="direct" apply="always" consider-accept-header="false"/>
            </when>
        </choose>
    </outbound>
</policies>

根据产品名称修改响应

以下示例演示了如何进行内容筛选,方法是:在使用 Starter 产品时删除从后端服务接收的响应中的数据元素。 示例后端响应包括类似于 OpenWeather One Call API 的根级别属性。

<!-- Copy this snippet into the outbound section to remove a number of data elements from the response received from the backend service based on the name of the product -->
<choose>
  <when condition="@(context.Response.StatusCode == 200 && context.Product.Name.Equals("Starter"))">
    <set-body>@{
        var response = context.Response.Body.As<JObject>();
        foreach (var key in new [] {"current", "minutely", "hourly", "daily", "alerts"}) {
          response.Property (key).Remove ();
        }
        return response.ToString();
      }
    </set-body>
  </when>
</choose>

后续步骤

有关使用策略的详细信息,请参阅: