简体   繁体   English

用于API版本控制的Web API 2属性路由

[英]Web API 2 Attribute Routing for API Versioning

I have an ASP.NET Web API 2 application which uses convention-based routing: 我有一个ASP.NET Web API 2应用程序,它使用基于约定的路由:

[app-url]/v1/[controller]

Now, I want to add another version to the API, and found that attribute routing is the easiest way to achieve what I want. 现在,我想在API中添加另一个版本,并发现属性路由是实现我想要的最简单方法。 Therefore, I registered the Web API routes in the Application_Start() method as shown below. 因此,我在Application_Start()方法中注册了Web API路由,如下所示。

GlobalConfiguration.Configure(WebApiConfig.Register);

Additionally, I made sure that the Register() method in the WebApiConfig class only contains the following line: 另外,我确保WebApiConfig类中的Register()方法仅包含以下行:

config.MapHttpAttributeRoutes();

There are two controllers to test the versioning: 有两个控制器来测试版本控制:

/v1/StatusController.cs /v1/StatusController.cs

[RoutePrefix("v1/status")]
public class StatusController : ApiController
{
    [Route("")]
    public string Get()
    {
        return "V1 - OK";
    }
}

/v2/StatusController.cs /v2/StatusController.cs

[RoutePrefix("v2/status")]
public class StatusController : ApiController
{
    [Route("")]
    public string Get()
    {
        return "V2 - OK";
    }
}

However, when I navigate to either of the routes shown below, I receive an error. 但是,当我导航到下面显示的任一路线时,我收到错误。

  1. [app-url]/v1/status [应用网址] / V1 /状态
  2. [app-url]/v2/status [应用网址] / V2 /状态

SERVER ERROR 服务器错误

404 - File or directory not found. 404 - 找不到文件或目录。

The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable. 您要查找的资源可能已被删除,名称已更改或暂时不可用。

Your approach will not work because ASP.NET Web API uses a so-called Controller Selector to find the respective controller for a given route. 您的方法将无法工作,因为ASP.NET Web API使用所谓的Controller Selector来查找给定路由的相应控制器。 This selector however, does not take into account the namespace of a controller, but rather uses its name. 但是,此选择器不考虑控制器的名称空间,而是使用其名称。

By default, Web API ships with the DefaultHttpControllerSelector and you can find information on its implementation here . 默认情况下,Web API附带DefaultHttpControllerSelector ,您可以在此处找到有关其实现的信息

A possible solution to your problem is to implement a custom IHttpControllerSelector and tell Web API to use it instead. 您的问题的一个可能的解决方案是实现自定义IHttpControllerSelector并告诉Web API使用它。 For this you can find several examples on the internet. 为此,您可以在互联网上找到几个例子。

Instead you can also change your controller names to be unique, eg: 相反,您也可以将控制器名称更改为唯一,例如:

  • StatusV1Controller
  • StatusV2Controller

Instead of using different namespaces, use different controller names (if this works for you). 不使用不同的命名空间,而是使用不同的控制器名称(如果这适用于您)。 If so, here is about how the code should look like: 如果是这样,这里是关于代码应该如何:

/v1/StatusController.cs: /v1/StatusController.cs:

[System.Web.Http.RoutePrefix("v1/status")]
public class StatusController : ApiController
{
    [System.Web.Http.Route("")]
    public string Get()
    {
        return "V1 - OK";
    }
 }

/v2/StatusController.cs: /v2/StatusController.cs:

[System.Web.Http.RoutePrefix("v2/status")]
public class Status2Controller : ApiController
{
    [System.Web.Http.Route("")]
    public string Get()
    {
        return "V2 - OK";
    }
 }

Now you would be able to invoke these 2 different versions by calling the Http Get Method like: 现在,您可以通过调用Http Get方法调用这两个不同的版本,如:

Https://YourUrl.com/v1/status

and: 和:

Https://YourUrl.com/v2/status

Hope this suggested alternative helps. 希望这建议替代帮助。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM