【问题标题】:MVC API Routing ParametersMVC API 路由参数
【发布时间】:2019-11-21 01:46:20
【问题描述】:

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

当我使用如下 URL 时: 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));
    }

}

【问题讨论】:

    标签: string model-view-controller parameters routes http-status-code-404


    【解决方案1】:

    通过使用修复

     [Route("GetPricesForRace")]
    

    而不是

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-01
      • 2017-04-09
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多