[英]adding new method to controller makes swagger give 500 error
嘗試這個;
[HttpGet("GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
[HttpGet("GetNumberOfForecasts/{amount}")]
public IEnumerable<WeatherForecast> GetAmount(int amount)
{
return Enumerable.Range(1, amount).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
僅供日后參考。
第 1 步:-> 打開您的開發工具,或按 F12。
Then open the network tab, and refresh your page.
第 2 步:> 單擊 swagger 調用並單擊響應選項卡。
你會看到你的錯誤:
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException:用於操作的沖突方法/路徑組合“GET WeatherForecast” - WebApplication1.Controllers.WeatherForecastController.Get (WebApplication1)、WebApplication1.Controllers.WeatherForecastController.GetAmount (WebApplication1)。 操作需要 Swagger/OpenAPI 3.0 的唯一方法/路徑組合。 使用 ConflictingActionsResolver 作為解決方法
嘗試這樣的路由
結果
將[Route("[controller]")]
更改為[Route("[controller]/[action]")]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.