APIM策略-无法强制转换对象



当我使用APIM URL重写策略时,我遇到了一个问题,我需要使用变量的后缀值。我尝试了各种方法,但最终总是出现错误"em>";无法强制转换"Microsoft"类型的对象。WindowsAzure。ApiManagement。代理网关。Pipeline响应"到类型"系统。字符串">

这是我的入境政策:

<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。然后在重写url策略中,将URL_Suffix强制转换为string

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

我修改了逻辑应用程序的输出,并将UR_Suffix从json主体中作为令牌(字符串(抛出,这显然解决了问题。

JSON输出:

{"suffix":"<value>"}

修改后的策略:

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

最新更新