问题描述
我具有以下HTTP触发的Azure函数。我已经使用此链接here为端点设置了Swagger。以下API需要一组查询字符串参数,即“ name”,“ email”,“ phone” ,因此它可以对目标对象进行一些搜索。目前,该功能的主体尚未实现,但这对这个问题无关紧要。
功能:
[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"]}");
}
注意:我不想使用路由值而不是查询字符串参数,因为具有这些参数是可选的,并且调用方可能不想提供其中之一。
例如,我尝试了以下操作,但是如果您删除任何参数,它将以404失败,因为它将参数作为路由的一部分(即使它将显示在Swagger中)
[FunctionName(nameof(GetBookingCalendarsFunction))]
public async Task<IActionResult> GetAllAsync(
[HttpTrigger(AuthorizationLevel.Anonymous,Route = "bookings/name={name}&email={email}&phone={phone}")] HttpRequest request,string name,string email,string phone,phone: {request.Query["phone"]}");
}
我已经在Google上搜索了几个小时,但到目前为止找不到任何有用的信息。感谢您的帮助。
解决方法
由于使用包AzureExtensions.Swashbuckle
将Swagger集成到Azure函数中,因此我们可以根据需要使用属性QueryStringParameter
来配置查询字符串。有关更多详细信息,请参阅here
例如
[FunctionName("GetBookingCalendarsFunction")]
[QueryStringParameter("name","this is name",DataType = typeof(string),Required = false)]
[QueryStringParameter("email","this is email",Required = false)]
[QueryStringParameter("phone","this is phone",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"]}");
}