1

我正在尝试按照本教程实现 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

您能否建议如何使控制器版本控制工作?

4

1 回答 1

1

我休息了一下,我记得在我的项目中,我有一个自定义的 IHttpControllerSelector 实现,它扩展了 DefaultHttpControllerSelector。

configuration.Services.Replace(typeof(IHttpControllerSelector), new ApiControllerSelector(config));

在我删除它之后,版本控制开始工作。执行 configuration.AddApiVersioning 在 ServicesContainer 中设置 ApiVersionControllerSelector。它被我的自定义实现意外替换。

于 2017-09-28T13:55:23.457 回答