SwaggerGenerator异常:操作错误的HTTP方法不明确

11dmarpk  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(179)

我有下面的控制器(测试目的,它不遵循REST与2个GET):

[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");
    }
}

我有两个GET方法,都没有任何参数。我尝试为每个方法添加一个Route和Name,但Swagger仍然显示错误:
异常错误:冲突的方法/路径组合“GET api/WeatherForecast”
我可以在 Postman 中使用以下两种方法:
天气预报网站
天气预报错误
swagger是否不允许这样做,因为它们都是GET,并且都不带任何参数,所以无法区分它们?名称或路由不够?

jljoyd4f

jljoyd4f1#

你可以试试

[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()
        {
            ......
        }

相关问题