我有 Swagger 的問題。
當我嘗試將查詢作為引數傳遞時,它會導致 URL 格式錯誤->
https://localhost:5001/api/sport/{id}
預期的結果是這樣的:
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 是如何生成的:每當我按下執行時,我都會收到一個未找到的錯誤,并且我的端點從未被呼叫。查看網路選項卡,我可以看到格式錯誤的網址:
https://localhost:5001/api/sport/{id}

uj5u.com熱心網友回復:
{id}url 解碼為{id},這使得“有意義”,您要求一個具有 Id 屬性的物件。這是一種 json-ish 的做法。
解決此問題的簡單方法是洗掉您的類并直接系結 Guid。
[HttpGet("{id:guid}")]
public async Task<ActionResult<SportEventResource>> Get([FromRoute] Guid id)
{
需要呼叫該引數,id因為這是模板 ( "{id:guid}") 中的內容。
uj5u.com熱心網友回復:
這似乎是由字母大小寫不匹配引起的 - URI 模板中的引數/api/sport/{id}命名為id(小寫),但方法引數命名為Id(大寫“I”)。
嘗試更改public Guid Id為public Guid id
或者
更改[HttpGet("{id:guid}")]為[HttpGet("{Id:guid}")].
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/371005.html
上一篇:如何讓二維陣列正確格式化?
