REST API的版本内置的ASP.NET MVC 3 - 最佳实践 [英] Versioning of REST API Built With ASP.NET MVC 3 - Best Practices

查看:108
本文介绍了REST API的版本内置的ASP.NET MVC 3 - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有用于创建与ASP.NET MVC 3 REST API的最佳做法?此刻我想开创了REST API的每个版本的控制器。例如,到目前为止,我有:

I am wondering if there is a best practice for creating a REST API with ASP.NET MVC 3? At the moment I am thinking of creating a controller for each version of the REST API. For example, so far I have:

public class V1Controller : Controller
{
    public V1Controller()
    {
    }

    public ActionResult GetUser(string userId, IUserRepository userRepostory)
    {
        //code to pull data and convert to JSON string
        return View("Results");
    }

    public ActionResult GetUsersByGroup(string groupId, IUserRepository userRepostory)
    {
        //code to pull data and convert to JSON string
        return View("Results");
    }
}

那么对于我的看法覆盖_ViewStart.cshtml去除布局,然后我有Results.cshtml,只是输出是在控制器动作格式化,此时JSON数据。有一个控制器,每一个REST调用看起来有点过分,但它是我能想到的,这样我可以保持API的清洁不同版本的最好方法,这样,当谈到创建API第2版,我可以创建一个V2Controller,而不是打破现有的API给人们的时间切换到新的API。

Then for the views I overwrite the _ViewStart.cshtml to remove the layout and then I have Results.cshtml that just outputs the data that is formatted in the controller action, right now JSON. Having every single REST call in one controller seems like a bit too much but it is the best way I can think of so that I can keep clean separate versions of the API so that when it comes to creating version 2 of the API, I can create a V2Controller and not break the existing API to give people time to switch over to the new API.

有没有更好的方式来创建一个REST API的ASP.NET MVC 3?

Is there a better way to create a REST API with ASP.NET MVC 3?

推荐答案

我能找到使用MVC的使用领域的体面的解决办法。

I was able to find a decent solution using MVC's use of Areas.

首先,我想有我的API遵循这个URL定义:

First, I wanted to have my API follow this URL Definition:

http://[website]/[major_version]_[minor_version]/{controller}/{action}/...

我也想打破在单独的项目文件的不同版本,并在每个版本使用相同的控制器名称:

I also wanted to break up the different versions in separate Project files and use the same Controller names in each version:

"../v1_0/Orders/ViewOrders/.."  => "../v2_3/Orders/ViewOrders/.."

我搜索了一圈,发现与使用MVC区一个可行的解决方案。

I searched around and found a workable solution with the use of MVC Areas.

我创造了我的解决方案在新项目名为Api.Controllers.v1_0并作为一个测试,把一个 SystemController.cs 文件中有:

I created a new project in my solution called "Api.Controllers.v1_0" and, as a test, put a SystemController.cs file in there:

using System.Web.Mvc;

namespace Api.Controllers.v1_0
{
    public class SystemController : Controller
    {
        public ActionResult Index()
        {
            return new ContentResult() {Content = "VERSION 1.0"};
        }
    }
}

然后我加了 v1_0AreaRegistration.cs 文件:

using System.Web.Mvc;

namespace Api.Controllers.v1_0
{
    public class v1_0AreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get{ return "v1_0";}
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "v1_0",
                "v1_0/{controller}/{action}/{id}",
                new { controller = "System", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

我通过对含在里面相应的文件..v1_1项目相同的步骤走,增加了项目,引用了我的Api.WebMVC项目,是关闭和运行。

I walked through the same steps above for a "..v1_1" project with the corresponding files in there, added the projects as references into my "Api.Web" MVC project and was off and running.

这篇关于REST API的版本内置的ASP.NET MVC 3 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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