Azure API 管理从 Azure Functions 导入标签

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

我将 Azure-Functions 导入到 Api-Management。
我喜欢将标签添加到 Api 管理,以便我可以对我的功能进行分组。

有没有办法在添加到 Azure-Functions 的 C# 函数中添加标签,然后导入到 Api-Management ?

这里是Azure函数的示例代码。 我想添加标签? 有没有办法添加标签?

最终结果在 API 管理中。 这里我手动为函数添加了标签以进行分组。

谢谢,

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

我尝试了示例 C# Azure Functions 代码将标签导入到 Azure API 管理。

代码

using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp49
{
    public class BranchesFunction
    {
        private readonly ILogger<BranchesFunction> _logger;

        public BranchesFunction(ILogger<BranchesFunction> logger)
        {
            _logger = logger;
        }

        [Function("BranchesV1")]
        public HttpResponseData Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/branches")] HttpRequestData req,
            FunctionContext executionContext)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request for BranchesV1.");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to the BranchesV1 Azure Function!");

            return response;
        }
    }
}

swagger.json

{
  "openapi": "3.0.0",
  "info": {
    "title": "My API",
    "version": "1.0"
  },
  "paths": {
    "/v1/branches": {
      "get": {
        "tags": [ "branch" ],
        "summary": "BranchesV1",
        "operationId": "get-branchesv1",
        "responses": {
          "200": {
            "description": "Successful response"
          }
        }
      }
    }
  }
}

输出

运行成功如下,

enter image description here

使用上面的 URL,我在浏览器中得到如下输出:-

enter image description here

发布了上述代码到Azure Function应用程序,如下所示:-

enter image description here

函数代码在门户中的 Azure Function 中成功运行,如下所示:-

enter image description here

我将上述函数 swagger.json 文件添加到 OpenAPI 规范中,如下所示:-

enter image description here

enter image description here

使用 swagger.json 文件创建 API 后,我可以看到以下标签已添加到 API 的 OpenAPI 规范文件中。

enter image description here

enter image description here

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