Azure Functions 和 Swagger UI - 如何在 swagger UI 中显示查询字符串参数?

Azure Functions and Swagger UI - How to display query string paramters in swagger UI?

我有以下由 HTTP 触发的 Azure 函数。我已经使用 link here 为我的端点设置了 Swagger。下面的 API 需要一组查询字符串参数,即 "name", "email", "phone",因此它可以进行一些搜索目标对象。目前函数的主体当然没有实现,但这对这个问题来说无关紧要。

我的问题:如何在 swagger 中显示查询字符串参数 UI?

函数:

[FunctionName(nameof(GetBookingCalendarsFunction))]
 public async Task<IActionResult> GetAllAsync(
        [HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "bookings")] HttpRequest request,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        return new OkObjectResult($"Name: {request.Query["name"]}, email: {request.Query["email"]}, phone: {request.Query["phone"]}");
    }

招摇UI这个功能

注意:我不想使用路由值而不是查询字符串参数,因为这些参数是可选的,调用者可能不想提供一个其中

例如,我尝试了以下操作,但如果您删除任何参数,它将失败并显示 404,因为它将它们作为路由的一部分(即使它会在 Swagger 中显示它们)

  [FunctionName(nameof(GetBookingCalendarsFunction))]
    public async Task<IActionResult> GetAllAsync(
        [HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "bookings/name={name}&email={email}&phone={phone}")] HttpRequest request,
        string name, string email, string phone,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        return new OkObjectResult($"Name: {request.Query["name"]}, email: {request.Query["email"]}, phone: {request.Query["phone"]}");
    }

我已经在谷歌上搜索了几个小时,但到目前为止找不到任何有用的信息。感谢您的帮助。

由于您使用包AzureExtensions.Swashbuckle将Swagger集成到Azure功能中,我们可以使用属性QueryStringParameter根据您的需要配置查询字符串。详情请参考here

例如

 [FunctionName("GetBookingCalendarsFunction")]
        [QueryStringParameter("name", "this is name", DataType = typeof(string), Required = false)]
        [QueryStringParameter("email", "this is email", DataType = typeof(string), Required = false)]
        [QueryStringParameter("phone", "this is phone", DataType = typeof(string), Required = false)]
        public static async Task<IActionResult> GetAllAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "bookings")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

           

            return new OkObjectResult($"Name: {req.Query["name"]}, email: {req.Query["email"]}, phone: {req.Query["phone"]}");
        }