【发布时间】:2022-01-08 06:30:12
【问题描述】:
我对 Swagger 有意见。
当我尝试将查询作为参数传递时,它的 URL 格式不正确->
https://localhost:5001/api/sport/%7Bid%7D
预期的结果是这样的:
https://localhost:5001/api/sport/00000000-0000-0000-0000-000000000001
调用时到达了我的端点。
我有这个路由参数,我的对象看起来像这样:
[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(SportEventResource),(int)HttpStatusCode.OK)]
[SwaggerOperation(
Summary = "Gets a new Sport event",
Description = "Gets a new Sport event with its associative Markets and Selections",
OperationId = "sportEvent.get",
Tags = new[] { "SportEventEndpoints" })]
public async Task<ActionResult<SportEventResource>> Get([FromRoute]GetSportEvent query)
{...
// The parameter looks like this...
public class GetSportEvent : IQuery<Sport>
{
public Guid Id { get; set; }
}
// The rest is basically the auto generated values.
// Rest of Setup => Program.cs
builder.Services.AddSwaggerGen(opt =>
{
opt.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
opt.EnableAnnotations();
});
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
这是我的 Swagger 的生成方式: 每当我按下执行时,我都会收到一个未找到的错误,并且我的端点永远不会被调用。 查看网络选项卡,我可以看到格式错误的 url:
https://localhost:5001/api/sport/%7Bid%7D
【问题讨论】:
-
为什么你的方法参数是
GetSportEvent而不仅仅是Guid本身? -
老实说只是在尝试......