You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure APIM:无需创建专属路由,全局策略暴露API文档可行性问询

Azure APIM全局策略批量暴露API文档问题

我在Azure APIM后端托管了多个API,计划以编程方式暴露API文档,因此有如下疑问:是否可以通过全局策略拦截指向../<api-service-id>/openapi.json的请求,无需为每个API创建专属路由即可返回对应端点的文档?

以下是最初编写但未生效的策略代码:

<policies>
    <inbound>
        <!--Dynamically call the APIM Management API-->
        <choose>
            <when condition="@(context.Request.OriginalUrl.Path.EndsWith("/openapi.json", StringComparison.OrdinalIgnoreCase))">
                <send-request mode="new" response-variable-name="result" timeout="10" ignore-error="true">
                    <set-url>@("/subscriptions/my-sub/resourceGroups/my-reg/providers/Microsoft.ApiManagement/service/my-service"  + "/apis/" + context.Request.MatchedParameters.GetValueOrDefault("api-id","") + "?export=true&format=openapi&api-version=2022-09-01-preview")</set-url>
                    <set-method>GET</set-method>
                    <authentication-managed-identity resource="https://management.azure.com/" />
                </send-request>
                <!--Return the response-->
                <return-response>
                    <set-status code="200" reason="OK" />
                    <set-header name="Content-Type" exists-action="override">
                        <value>application/json</value>
                    </set-header>
                    <set-body>@((string)(((IResponse)context.Variables["result"]).Body.As<JObject>()["value"]))</set-body>
                </return-response>
            </when>
        </choose>
    </inbound>
    <backend>
        <forward-request />
    </backend>
    <outbound />
    <on-error />
</policies>

编辑说明:在Ikhtesam Afrin的帮助下,代码已可正常运行,但仍未解决“需要为每个API创建端点”的核心问题,修改后的<set-body>代码片段如下:

<set-body>@(((IResponse)context.Variables["result"]).Body.As<JObject>().ToString())</set-body>
</return-response>

内容的提问来源于stack exchange,提问作者Bennimi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 22:47:05