嗨,下面是我的场景
我有方法为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/人和V2 POST/人运行良好,但
当我根据我的要求将CreatePerson的V2路由从"/perse"更改为"/persons"时,我在从swagger 调用V1 POST/person时出现以下错误
Microsoft.AspNetCore.Routing.Matching.AmbiqueMatchException:请求与多个终结点匹配。Controllers.PersonControllerV2.CreatePersonControllers.PersonController.CreatePerson
在这种情况下,我的
V1招摇显示
- 帖子/人
- PUT/人
V2型锻显示
- 岗位/人员
- 岗位/人
- PUT/人
有解决这个问题的想法或建议吗?
[ApiVersion(ApiControllerVersion.Version1)]
和[ApiVersion(ApiControllerVersion.Version2)]
之前指定[ApiController]
。
所以你应该这样改变:
[ApiController]
[ApiVersion(ApiControllerVersion.Version1)]
public class PersonController : BaseController
{...
[ApiController]
[ApiVersion(ApiControllerVersion.Version2)]
public class PersonControllerV2 : PersonController
{...