控制流程

適用於:所有 APIM 層

利用 choose 原則,根據布林運算式的評估結果,有條件地套用原則陳述式。 使用類似程式設計語言中 if-then-else 或 switch 構造的控制流程原則。

注意

請依照原則陳述式中提供的順序,來設定原則的元素和子元素。 深入了解如何設定或編輯 APIM 原則。

原則陳述式

<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/> 元素中的條件會依照其在原則內的外觀順序進行評估。 系統會套用條件屬性等於 <when/> 的第一個 true 元素內所括住的原則陳述式。 如果所有 <otherwise/> 元素的條件屬性都是 <when/>,則會套用 false 元素內所括住的原則 (如果有的話)。

元素

元素 描述 必要
當… 一或多個指定 if 原則 ifelse 或 choose 部分的元素。 如果指定了多個 when 元素,則會循序進行評估。 一旦 when 元素的 condition 評估為 true 後,就不會再評估後面的 when 條件。 是的
否則 若沒有任何 when 條件評估為 true 時,所要評估的原則片段。 否

when 屬性

屬性 描述 必要
條件 包含 when 的原則陳述式受評估時,須評估的布林運算式或布林常數。 是的

使用方式

範例

根據使用者代理程式修改要求與回應

下列範例會示範 set-variable 原則和兩個控制流程原則。

設定變數原則位於 inbound 區段中,並且會在 isMobile 要求標頭包含文字 或 User-Agent 時,建立設為 true 的 iPad 布林iPhone變數。

第一個控制流程原則也位於 inbound 區段中,並且會按照條件,根據 內容變數的值套用兩個isMobile原則的其中一個。

第二個控制流程原則位於 outbound 區段中,並且會按照條件,在 設為 isMobile 時套用true 原則。

<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 呼叫 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>

如需使用原則的詳細資訊,請參閱: