我有以下控制器(測驗目的,它不遵循 2 GET 的 REST):
[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly IWeatherService _weatherService;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IWeatherService weatherService)
{
_logger = logger;
_weatherService = weatherService;
}
[HttpGet(Name = "GetWeatherSummary")]
[Route("getweatherforecast")]
public IEnumerable<WeatherForecast> Get()
{
return _weatherService.GetWeatherSummary();
}
[HttpGet(Name = "Error")]
[Route("error")]
public Task<IActionResult> Error()
{
throw new Exception("some error message");
}
}
我在那里有 2 個 GET 方法,都沒有使用任何引數。我嘗試為每個路由和名稱添加一個路由,但 Swagger 仍然顯示錯誤:
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException:沖突的方法/路徑組合“GET api/WeatherForecast”
我可以在郵遞員中使用:
http://localhost:5000/api/WeatherForecast/getweatherforecast
http://localhost:5000/api/WeatherForecast/error
swagger 是否不允許這樣做,因為它們都是 GET 并且都不帶任何引數,所以它無法區分它們?Name 或 Route 還不夠?
uj5u.com熱心網友回復:
你可以試試
[HttpGet(Name = "GetWeatherSummary")]
public IEnumerable<WeatherForecast> Get()
{
......
}
[HttpGet(Name = "Error")]
public Task<IActionResult> Error()
{
......
}
或者
[HttpGet]
[Route("getweatherforecast")]
public IEnumerable<WeatherForecast> Get()
{
......
}
[HttpGet]
[Route("error")]
public Task<IActionResult> Error()
{
......
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/508236.html
標籤:。网 asp.net 核心 asp.net-web-api 昂首阔步 花花公子
上一篇:發布后如何更新模型屬性值?
下一篇:如何清理C#方法中的重復元素?