我需要在下面的下拉列表中显示选项 Hostname1、Hostname2 和 Hostname3,但它显示的是选项 0、1 和 2。代码是使用 Open API、.NET 6 版本在 Azure Function Http Trigger 中编写的。
下面是我正在使用的代码:
Azure 功能:
public enum Hostname
{
Hostname1,
HostName2,
HostName3,
}
public class OpenAPIFunction
{
private readonly HttpClient _httpClient;
private readonly ILogger<OpenAPIFunction> _logger;
public OpenAPIFunction(ILogger<OpenAPIFunction> log, IHttpClientFactory httpClientFactory)
{
_logger = log;
_httpClient = httpClientFactory.CreateClient();
}
[FunctionName("OpenAPIFunction")]
[OpenApiOperation(operationId: "Run", tags: new[] { "name" })]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(Hostname), Description = "The **Name** parameter. Allowed values: `Hostname1`, `HostName2`, `HostName3`")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", "post", Route = null)] Hostname req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
//string name = req.Query["name"];
//string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
//dynamic data = JsonConvert.DeserializeObject(requestBody);
//name ??= data?.name;
string responseMessage = string.IsNullOrEmpty("")
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {""}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
Startup.cs
:
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SchemaFilter<EnumSchemaFilter>();
});
}
}
过滤器:
public class EnumSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema openApiSchema, SchemaFilterContext schemaFilterContext)
{
if (schemaFilterContext.Type.IsEnum)
{
openApiSchema.Type = "string";
openApiSchema.Enum.Clear();
var enumValues = Enum.GetNames(schemaFilterContext.Type);
foreach (var enumValue in enumValues)
{
openApiSchema.Enum.Add(new OpenApiString(enumValue));
}
}
}
}
我尝试遵循此线程,但无法弄清楚我做错了什么。任何帮助将不胜感激。
在
Enum中添加
[JsonConverter(typeof(StringEnumConverter))]
。
[JsonConverter(typeof(StringEnumConverter))]
public enum Hostname
{
Hostname1,
HostName2,
HostName3,
}
我在 OpenAPIFunction 文件中有以下代码。
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace _78461112
{
public class OpenAPIFunction
{
private readonly HttpClient _httpClient;
private readonly ILogger<OpenAPIFunction> _logger;
public OpenAPIFunction(ILogger<OpenAPIFunction> log, IHttpClientFactory httpClientFactory)
{
_logger = log;
_httpClient = httpClientFactory.CreateClient();
}
[FunctionName("OpenAPIFunction")]
[OpenApiOperation(operationId: "Run", tags: new[] { "name" })]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(Hostname), Description = "The **Name** parameter. Allowed values: `Hostname1`, `HostName2`, `HostName3`")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
[JsonConverter(typeof(StringEnumConverter))]
public enum Hostname
{
Hostname1,
HostName2,
HostName3,
}
}
我可以在下拉列表中获取主机名。