【问题标题】:ASP.NET WebApi versioning not workingASP.NET WebApi 版本控制不起作用
【发布时间】:2018-03-10 04:58:34
【问题描述】:

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

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

【问题讨论】:

  • 您的“V1”控制器可以有多个版本? V1 控制器会是一个版本吗? ...为什么有两个属性?
  • 这可能是您网址中的点的问题。
  • 我的 api 由几个控制器组成。虽然版本 2 影响了其他控制器,但没有对 MyV1Controller 进行任何更改。所以我只是增加了数量。

标签: c# asp.net .net asp.net-web-api api-versioning


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 2020-06-27
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多