我正在尝试按照本教程实现api版本控制。所以在我的创业公司中我有:
var constraintResolver = new DefaultInlineConstraintResolver()
{
ConstraintMap =
{
["apiVersion"] = typeof( ApiVersionRouteConstraint )
}
};
configuration.MapHttpAttributeRoutes(constraintResolver);
configuration.AddApiVersioning()
和我的控制员:
[Route("api/v{version:apiVersion}/my")]
[ApiVersion("1.0")]
[ApiVersion("2.0")]
public class MyV1Controller
[Route("api/v{version:apiVersion}/my")]
[ApiVersion("3.0")]
public class MyV3Controller
当我请求http://localhost/api/v1.0/my时,我收到错误
Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.\r\n\r\nThe request has found the following matching controller types: \r\nMyV1Controller\r\nMyV2Controller
请问如何让控制器版本化工作?
答案 0 :(得分:1)
我休息一下,我记得在我的项目中我有一个自定义的IHttpControllerSelector实现,它扩展了DefaultHttpControllerSelector。
configuration.Services.Replace(typeof(IHttpControllerSelector), new ApiControllerSelector(config));
我删除后,版本开始工作。 执行configuration.AddApiVersioning在ServicesContainer中设置ApiVersionControllerSelector。它被意外地替换为我的自定义实现。