Web API 2中的版本控制 [英] Versioning in Web API 2

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

问题描述

关于Web Api 2.0中的版本控制有一个简短的问题.

Had a quick question regarding versioning in Web Api 2.0.

我已经开发了一个完全RESTful API,但是我有2个同名控制器.

I've developed an entirely RESTful API, however I have 2 controllers of the same name.

我已经根据单独的文件夹将它们放在diff名称空间中. (例如v1和v2文件夹).

I've put them in diff namespaces according to separate folders. (E.g. v1 and v2 folders).

我还使用普通的NamespaceHttpControllerSelector配置了正确的路由. 到目前为止,一切都正常工作.

I've also configured the correct routing using the normal NamespaceHttpControllerSelector. So up until now everything is working correctly.

但是我有一个问题...即使我将 EmailsController放在根目录级别,也可以仍然对电子邮件控制器进行版本控制吗?

I had a question however...is it possible to still have versioning for the Emails controller even if I put EmailsController on the Root level?

例如

Controllers/v1/EmailControllers

Controllers/v1/EmailControllers

控制器/电子邮件控制器

Controllers/EmailsControllers

这仍然有可能吗?

我忘了提到我没有使用RoutePrefix属性,因为我一直在接收RouteConstraint上的错误.

I forgot to mention that I have not used the RoutePrefix attribute because I kept on receiving errors on my RouteConstraint.

我已经用以下内容装饰了我的操作方法(这似乎有点多余,因为它们本质上具有相同的命名空间):

I've decorated my action methods with the following (which seems a bit redundant because they essentially have the same Namespace):

[Route("api/{apiVersion1:ApiVersion1Constraint(v1)}/emails/LogEmails", Name = "LogEmailsRoute_1")]

推荐答案

您是否尝试过使用[ApiVersion]属性

Have you tried with [ApiVersion] attribute

    [ApiVersion( "1.0", Deprecated = true )]    
    [ApiVersion( "2.0" )]
    [ApiVersion( "3.0" )]
    [Route( "api/v{version:apiVersion}/email" )]
    public class Email2Controller : Controller {

    [HttpGet]
    public string Get() => "Email  v2!";

    [HttpGet, MapToApiVersion( "3.0" )]
    public string GetV3() => "Email v3!";

    }

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

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