ASP.NET Core Web API-AmbiguousMatchException:该请求与多个端点匹配

时间:2020-11-08 20:55:05

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

我一直在研究,但是仍然找不到解决方案,

要返回所有玩家,我将传递类似以下内容: http:// localhost:7777 / api / teams / 34fe3b6f-ba23-4657-820a-6c59dd49173a / players

要返回特定团队中的特定球员,我将传递类似于以下内容的内容: http:// localhost:7777 / api / teams / 34fe3b6f-ba23-4657-820a-6c59dd49173a / players / f7de7974-9cbb-4c2c-884e-29036d6c2d76

我不断收到以下错误: System.ArgumentException:'路由参数名称'id'在路由模板中出现多次。 '

有人可以建议如何解决此问题吗?

[Route("api/Teams/{Id}/Players}")]
[ApiController]
public class PlayersController : ControllerBase
{

    [HttpGet]
    public IActionResult GetAllTeamPlayers(Guid id)
    {
        return Ok();
    }

    [HttpGet]
    public IActionResult GetTeamPlayer(Guid id, Guid id2)
    {
        return Ok();
    }
}

1 个答案:

答案 0 :(得分:1)

您应该这样定义路线参数:

[Route("api/Teams/{teamId}/}")]
[ApiController]
public class PlayersController : ControllerBase
{

    [HttpGet("players")]
    public IActionResult GetAllTeamPlayers([FromRoute] Guid teamId)
    {
        return Ok();
    }

    [HttpGet("players/{playerId}")]
    public IActionResult GetTeamPlayer([FromRoute] Guid teamId, [FromRoute] Guid playerId)
    {
        return Ok();
    }
}