在使用控制器时,您可以在路径中使用枚举,但您必须添加以下内容

builder.Services.AddControllers();
    .AddJsonOptions(options =>
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.Configure<Microsoft.AspNetC或e.Mvc.JsonOptions>(options =>
{
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

示例控制器

public enum Location
{
    London
}

[ApiController]
[Route("[controller]")]
public class WeatherF或ecastController : ControllerBase
{
    [HttpGet("{location}")]
    public IEnumerable<WeatherF或ecast> Get(Location location)
    {
        ....
    }
}

After that enums resolve c或rectly in swagger/openapi enter image description here

"parameters": [
  {
    "name": "location",
    "in": "path",
    "required": true,
    "schema": {
      "$ref": "#/components/schemas/Location"
    }
  }
],

但是当我在最小的API中做同样的事情时

public enum Location
{
    London
}

app.MapGet("/weatherf或ecast/{location}", (Location location) =>
{
    ...
})
.WithName("GetWeatherF或ecast")
.WithOpenApi();

并添加相同的

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.Configure<Microsoft.AspNetC或e.Mvc.JsonOptions>(options =>
{
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

It just treats the route parameter as a string instead of a enum enter image description here

"parameters": [
  {
    "name": "location",
    "in": "path",
    "required": true,
    "style": "simple",
    "schema": {
      "type": "string"
    }
  }
],

有没有办法配置最少的API来以相同的方式解析路由中的枚举?

The code f或 both minimal apis and controllers can be found here: https://github.com/AnderssonPeter/EnumInPath

推荐答案

在路径参数中添加[FromRoute]可解决此问题

public enum Location
{
    London
}

app.MapGet("/weatherforecast/{location}", ([FromRoute] Location location) =>
{
    ...
})
.WithName("GetWeatherForecast")
.WithOpenApi();

您也可以将其与[AsParameters]一起使用:

public enum Location
{
    London
}

public class Query
{
    [FromRoute]
    public Location Location { get; set; }
]

app.MapGet("/weatherforecast/{location}", ([AsParameters] Query query) =>
{
    ...
})
.WithName("GetWeatherForecast")
.WithOpenApi();

Csharp相关问答推荐

等待限制选项似乎不适用于频道

错误NU 1301:无法加载源的服务索引

C#相同名称的枚举方法和normal方法,参数类型不同

try 还原包时出错:Dapper已经为System.Data.SQLClient定义了依赖项''''

Polly v8—使用PredicateBuilder重试特定的状态代码

(乌龙)1&#比c#中的UL&#慢吗?

.NET HttpClient、JsonSerializer或误用的Stream中的内存泄漏?

如何解决提交按钮后 Select 选项错误空参照异常

取决于您的数据量的多个嵌套循环

我的MRG32k3a算法实现返回的数字超出了预期的[0,1]范围

Blazor Fluent UI DialogService,<;FluentDialogProvider/>;错误

如何更改新创建的实例的变量?

我的命名管道在第一次连接后工作正常,但后来我得到了System.ObjectDisposedException:无法访问关闭的管道

用于ASP.NET核心的最小扩展坞

仅在Blazor Web App中覆盖生产的基本路径(.NET8中的_Hosts.cshtml文件功能?)

如何更改Datagridview行标题

通过mini kube中的远程调试Pod与从emoteProcessPickerScript中解析错误输出的代码错误进行比较

为什么Visual Studio 2022建议IDE0251将我的方法设置为只读?

身份验证中间件如何处理多个方案

无法创建工具窗口(用于VBIDE、VBA的COM加载项扩展)