Web API 2属性路由:AmbiguousMatchException:请求与多个端点匹配

问题描述 投票:0回答:1

您好,这是我的情况

我有带方法CreatePerson的PersonController

[ApiVersion(ApiControllerVersion.Version1)]
    public class PersonController : BaseController
    {
        [HttpPost]
        [Route("/person")]
        [Produces("application/json")]
        [SwaggerResponse((int)HttpStatusCode.OK)]
        [SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(ErrorServiceResponse))]
        [SwaggerResponse((int)HttpStatusCode.BadRequest, Type = typeof(ErrorServiceResponse))]
        [SwaggerOperation("POST: Create Person")]
        [ValidateModelState]
        public async Task<IActionResult> CreatePerson([FromBody][Required] CreatePersonRequest person)
        {
        }

        [HttpPut]
        [Route("/person")]
        [Produces("application/json")]
        [SwaggerResponse((int)HttpStatusCode.OK)]
        [SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(ErrorServiceResponse))]
        [SwaggerResponse((int)HttpStatusCode.BadRequest, Type = typeof(ErrorServiceResponse))]
        [SwaggerOperation("POST: Change Person")]
        [ValidateModelState]
        public async Task<IActionResult> UpdatePerson([FromBody][Required] UpdatePersonRequest person)
        {
        }
    }

我想创建此API的另一个版本,因此创建如下,因为我的创建路线已更改。我要继承PersonController的地方,我需要V1的所有方法并从v2更改为“ CreatePerson”之外的所有方法]

[ApiVersion(ApiControllerVersion.Version2)]
    public class PersonControllerV2 : PersonController
    {
        [HttpPost]
        [Route("/persons")]
        [Produces("application/json")]
        [SwaggerResponse((int)HttpStatusCode.OK)]
        [SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(ErrorServiceResponse))]
        [SwaggerResponse((int)HttpStatusCode.BadRequest, Type = typeof(ErrorServiceResponse))]
        [SwaggerOperation("POST: Create Person")]
        [ValidateModelState]
        public new async Task<IActionResult> CreatePerson([FromBody][Required] CreatePersonRequest person)
        {
        }
    }

如果我不更改路线,则可以看到V1 POST / person和V2 POST / person运行正常,但是>

当我根据需要将CreatePerson的V2路由从“ / person”更改为“ / persons”时,我从招摇中调用V1 POST / person时遇到了以下错误

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:该请求与多个端点匹配。Controllers.PersonControllerV2.CreatePersonControllers.PersonController.CreatePerson

在这种情况下是我的

V1大摇大摆显示

  • POST /人
  • PUT /人
  • V2大摇大摆显示

  • POST /人
  • POST /人
  • PUT /人
  • 是否有解决此问题的想法或建议?

[嗨,下面是我的场景,我的PersonController带有CreatePerson方法[ApiVersion(ApiControllerVersion.Version1)]公共类PersonController:BaseController {[HttpPost] ...

routes asp.net-web-api2 asp.net-core-webapi swagger-ui
1个回答
0
投票

原因是因为您没有在[ApiController][ApiVersion(ApiControllerVersion.Version1)]之前指定[ApiVersion(ApiControllerVersion.Version2)]

© www.soinside.com 2019 - 2024. All rights reserved.