MVC API 路由参数



我在.NET Core 2.2上有一个MVC Web API

当我使用网址时,例如:https://localhost:44374/HorseRacingApi/prices/GetPricesForRace/2019-07-11T00:00:00/14/1

它工作正常,但是当我像查询字符串一样使用 URL 时:https://localhost:44374/HorseRacingApi/prices/GetPricesForRace?meetingDate=2019-07-11T00:00:00&courseId=14&raceNumber=1

我收到 404 错误,有没有办法解决这个问题?

以下是以下控制器和路由设置:

[Route("HorseRacingApi/[controller]")]
[Produces("application/json")]
[ApiController]
public class PricesController : Controller
{
    public IPriceService _priceService;
    public PricesController(IPriceService priceService)
    {
        _priceService = priceService;
    }
    [HttpGet]
 [Route("GetPricesForRace/{meetingDate}/{courseId}/{raceNumber}/{ShowAll?}")]
    public IActionResult GetPricesForRace(DateTime meetingDate, int courseId, int raceNumber, bool? ShowAll = false)
    {
         return Ok(_priceService.GetPricesForRace(meetingDate, courseId, raceNumber));
    }
}

使用

修复
 [Route("GetPricesForRace")]

而不是

[Route("GetPricesForRace/{meetingDate}/{courseId}/{raceNumber}/{ShowAll?}")]

我现在可以像查询字符串一样使用。

最新更新