Azure APIM:检查参数是否已存在于字典中

问题描述 投票:0回答:1

在 APIM 中,其中一个 API 内,我在“所有操作”内有一个与此类似的策略:

<policies>
    <inbound>
        <base />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Variables["xvar"] == null ||  (string)context.Variables["xvar"] == "oneValue")">
                <trace source="info" severity="information">
                    <message>Hi, xvar exist</message>
                </trace>
            </when>
            <otherwise />
        </choose>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

变量

xvar
确实存在于该 API 中的少数操作中,例如t1。那里的政策看起来与此类似:

<policies>
    <inbound>
        <set-variable name="xvar" value="oneValue" />
        <base />
        <set-backend-service base-url="https://testingtest.com" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

但是,此 API 中的大多数操作中并不存在此变量,例如t2。因此,当我调用此操作时,我收到此错误:

choose (1.469 ms)
{
    "messages": [
        {
            "message": "Expression evaluation failed.",
            "expression": "context.Variables[\"xvar\"] == null ||  (string)context.Variables[\"xvar\"] == \"oneValue\"",
            "details": "The given key was not present in the dictionary.\r\n   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)"
        },
        "Expression evaluation failed. The given key was not present in the dictionary.\r\n   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)",
        "The given key was not present in the dictionary."
    ]
}

我应该改变“所有操作”中的条件来检查字典中是否存在或不存在变量

azure api azure-api-management azure-api-apps
1个回答
0
投票

请使用以下表达式检查所有操作中变量是否存在:

context.Variables.ContainsKey("xvar")

所有操作 - 政策:

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="http://echoapi.cloudapp.net/api" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Variables.ContainsKey("xvar") && !context.Variables.GetValueOrDefault<string>("xvar").Equals(string.Empty))">
                <trace source="info" severity="information">
                    <message>Hi, xvar exist</message>
                    <metadata name="xvar" value="@(context.Variables.GetValueOrDefault<string>("xvar"))" />
                </trace>
            </when>
            <otherwise />
        </choose>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

t1 - 政策:

<policies>
    <inbound>
        <set-variable name="xvar" value="oneValue" />
        <base />
        <set-backend-service base-url="http://echoapi.cloudapp.net/api" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

t2 - 政策:

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="http://echoapi.cloudapp.net/api" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

这会跟踪 t1t2 的以下消息:

出站 t1 - 选择:

{
    "message": "Expression was successfully evaluated.",
    "expression": "context.Variables.ContainsKey(\"xvar\") && !context.Variables.GetValueOrDefault<string>(\"xvar\").Equals(string.Empty)",
    "value": true
}

出站 t1 - 追踪:

{
    "message": "Expression was successfully evaluated.",
    "expression": "context.Variables.GetValueOrDefault<string>(\"xvar\")",
    "value": "oneValue"
}

出境t2 - 选择:

{
    "message": "Expression was successfully evaluated.",
    "expression": "context.Variables.ContainsKey(\"xvar\") && !context.Variables.GetValueOrDefault<string>(\"xvar\").Equals(string.Empty)",
    "value": false
}

© www.soinside.com 2019 - 2024. All rights reserved.