ASP.NET API版本控制 [英] ASP.NET API versioning

查看:128
本文介绍了ASP.NET API版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET的新手,但我希望为即将开始的新API实现一些版本控制.

I'm new to ASP.NET but I'm looking to implement some versioning for a new API I'm about to start.

我什至不确定我要寻找的内容是否可行,但是我正在使用一种非常干净的使用标头变量的版本方法.

I'm not even sure if what I'm looking for is possible but I'm after a very clean version method using a header variable.

理想情况下,我希望能够在代码结构中包含一个版本文件夹,并在其中包含不同的API版本的不同文件夹. 每个版本文件夹将包含核心API代码的完整副本,因此我想永远不会有任何冲突等.我知道这会使代码膨胀,但是值得保持非常整洁,并且只有2-3个以上API版本已启用.

Ideally I want to be able to have a versions folder within the code structure and different folders containing the different API versions within that. Each version folder would contain a full copy of the core API code so I'd know there would never be any conflicts etc. I know this would inflate the code but it's worth to keep it very clean and there would only be over 2-3 versions of the API active.

我在Internet上发现了许多标头示例,但是它们都要求这些类位于不同的命名空间中,如果我要编写完整的代码副本,那么每次都必须重命名所有类是不现实的.被复制.

I've found many header samples on the Internet but they all require the classes to be in different namespaces and if I'm doing a complete copy of the code then it's not practical to have to rename all the classes each time they are copied.

我想做的是可能的吗?还是在处理多个类时有更清洁的解决方案?

Is what I'm trying to do possible? Or is there a cleaner solution when dealing with multiple classes?

推荐答案

有四种基本方法可以对RESTful方法进行版本控制-

There are four basic approaches to version the RESTful way -

  1. URI路径这种方法采用以下形式:

  1. URI Path This approach takes the following form:

http://api/v2/Tasks/ {TaskId}

http://api/v2/Tasks/{TaskId}

URI参数这种方法采用以下形式:

URI Parameter This approach takes the following form:

http://api/Tasks/ {TaskId}?v = 2

http://api/Tasks/{TaskId}?v=2

内容协商这是在HTTP标头中完成的.

Content Negotiation This is done in the HTTP header.

内容类型:application/vnd.taskManagerApp.v2.param.json

请求标头也可以在HTTP标头中完成.

Request Header This is also done in the HTTP header.

x-taskManagerApp版本:2

我个人喜欢第一种方法.您可以阅读迈克·沃森(Mike Wasson)的ASP.NET Web API:使用命名空间进行版本控制Web API .

I personally like 1st approach. You can read Mike Wasson's ASP.NET Web API: Using Namespaces to Version Web APIs.

许多人都修改了Mike Wasson的原始资料.我喜欢Jamie在 ASP.NET Web API 2书中使用的那本书库尔兹,布莱恩·沃特曼(Brian Wortman).

Many people have modified Mike Wasson's Original Source. I like the one used in ASP.NET Web API 2 book by Jamie Kurtz, Brian Wortman.

由于有太多可移动的部分,我在GitHub上创建了一个示例项目 > .

Since it has too many moving pieces, I created a sample project at GitHub.

config.Routes.MapHttpRoute(
   name: "DefaultApi",
   routeTemplate: "api/{version}/{controller}",
   defaults: new { version = "v2" }
);

config.Routes.MapHttpRoute(
   name: "DefaultApiWithId",
   routeTemplate: "api/{version}/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional }
);

然后,您添加 ApiVersionConstraint

public class ApiVersionConstraint : IHttpRouteConstraint
{
    public ApiVersionConstraint(string allowedVersion)
    {
        AllowedVersion = allowedVersion.ToLowerInvariant();
    }

    public string AllowedVersion { get; private set; }

    public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName,
        IDictionary<string, object> values, HttpRouteDirection routeDirection)
    {
        object value;
        if (values.TryGetValue(parameterName, out value) && value != null)
        {
            return AllowedVersion.Equals(value.ToString().ToLowerInvariant());
        }
        return false;
    }
}

用法

您只需将 RoutePrefix 放在控制器上,就可以完成.

Usage

You just place RoutePrefix on a controller, and you are done.

[RoutePrefix("api/{apiVersion:apiVersionConstraint(v1)}/values")]
public class ValuesController : ApiController
{
    // GET api/v1/values
    [Route("")]
    public IEnumerable<string> Get()
    {
        return new string[] { "v1-value1", "v1-value2" };
    }

    // GET api/v1/values/5
    [Route("{id}")]
    public string Get(int id)
    {
        return "v1-value-" + id;
    }
}

这篇关于ASP.NET API版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆