.Net Core API:没有路由与提供的值匹配

时间:2020-08-07 07:35:49

标签: .net api .net-core asp.net-core-webapi

我已经这样做了几个小时。我在这里做什么错了?

BaseController路由

[Route("api/v{v:apiVersion}/[controller]")]
[ApiController]
[Authorize]
public class BaseControllerV2 : ControllerBase
{
}

控制器路线

public class OrdersController : BaseControllerV2
{

private readonly IOrderService _iOrderService;

public OrdersController(IOrderService iOrderService)
{
    _iOrderService = iOrderService;
}

  

  /// <summary>
    /// Retrieve order details
    /// </summary>
    [HttpGet("{orderNo:int}", Name = nameof(GetOrderByOrderNo))]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult<Business.Models.V2.Order>> GetOrderByOrderNo([FromRoute]int orderNo, [BindRequired, FromQuery]int agencyNo, CancellationToken cancellationToken)
    {
        var result = await _iOrderService.GetOrderByOrderNo(orderNo, agencyNo, cancellationToken);

        if (result == null) return NotFound();

        return Ok(result);
    }

    /// <summary>
    /// Create quote
    /// </summary>
    [HttpPost(Name = nameof(CreateQuote))]
    [ProducesResponseType(StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult<Business.Models.V2.Order>> CreateQuote([FromBody]Business.Models.V2.Order order, CancellationToken cancellationToken)
    {
        var result = await _iOrderService.CreateQuote(order, cancellationToken);

        return CreatedAtAction(nameof(GetOrderByOrderNo), new { orderNo = result.OrderNo, agencyNo = result.AgencyNo, cancellationToken}, result);
    }

}

我已经尝试了其他相关问题的解决方案,但CreatedAtAction仍然找不到端点。

2 个答案:

答案 0 :(得分:0)

看起来您的GetOrderByOrderNo仅接受1个参数HttpGet("{orderNo:int}",但是您尝试传递3个参数,并将其他2个参数添加到HttpGet

答案 1 :(得分:0)

您缺少控制器路由中的一个参数-{v:apiVersion},请尝试以下操作:

    public async Task<ActionResult<Business.Models.V2.Order>> CreateQuote([FromRoute] ApiVersion version, [FromBody]Business.Models.V2.Order order, CancellationToken cancellationToken)
    {
        var result = await _iOrderService.CreateQuote(order, cancellationToken);

        return CreatedAtAction(nameof(GetOrderByOrderNo), new { v = version.ToString(), orderNo = result.OrderNo, agencyNo = result.AgencyNo, cancellationToken}, result);
    }

在这里您可以找到示例:https://github.com/microsoft/aspnet-api-versioning/blob/master/samples/aspnetcore/BasicSample/Controllers/HelloWorldController.cs