APIM 策略 - 无法投射对象

APIM Policy - Unable to cast object

我在使用 APIM URL 重写策略时遇到问题,我需要使用变量中的后缀值。我尝试了各种方法,但总是以错误 "Unable to cast object of type 'Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.PipelineResponse' to type 'System.String"

这是我的入境政策:

 <inbound>
    <!-- Send Requst to Logic Apps to retrieve the corresponding suffix -->
    <send-request mode="new" response-variable-name="Response_Suffix" timeout="20" ignore-error="true">
        <set-url>{{LogicAppsURL}}</set-url>
        <set-method>POST</set-method>
        <set-header name="Content-Type" exists-action="append">
            <value>application/json</value>
        </set-header>
        <set-body>@{
                
                return new JObject(
                    new JProperty("ModelName", (context.Request.Body.As<JObject>(preserveContent: true).SelectToken("ModelName")))).ToString();

                }</set-body>
    </send-request>
    <set-variable name="URL_Suffix" value="@((IResponse)context.Variables["Response_Suffix"])" />

    <!-- Set Backend URL -->
    <set-backend-service base-url="{{BaseURL}}" />
    <base />

    <!-- Rewrite URL -->
    <rewrite-uri template="@((string)context.Variables["URL_Suffix"])" />
</inbound>

Response_Suffix 变量根据 send-request 策略设置为 IResponse 实例。然后您的设置变量策略也将 URL_Suffix 变量设置为 IResponse。然后在 rewrite-url 策略中将 URL_Suffix 转换为 string.

不确定 URL 后缀的确切含义,但您的设置变量策略可能是错误的,感觉您应该从响应中提取一些 属性,而不是整个响应。

我修改了 Logic Apps 的输出,并将 UR_Suffix 作为标记(字符串)从 json 正文中删除,这显然解决了问题。

JSON 输出:

{"suffix":"<value>"}

修改后的政策:

 <set-variable name="URL_Suffix" value="@((((IResponse)context.Variables["Suffix_Body"]).Body.As<JObject>().SelectToken("suffix")).ToString())" />