简体   繁体   English

MVC3 REST路由和Http动词

[英]MVC3 REST Routes & Http Verbs

As a result of a previous question of mine, I have discovered two ways of handling REST routes in MVC3. 作为我之前的一个问题的结果,我发现了两种在MVC3中处理REST路由的方法。

This is a followup question where I am trying to learn factual differences/subtleties between these two approaches. 这是一个后续问题,我试图了解这两种方法之间的事实差异/细微差别。 I am looking authoritative answer if possible. 如果可能的话,我正在寻找权威的答案。

Method 1: Single Route, with Action Name + Http Verb Attributes on Controller Actions 方法1:单一路由,在控制器操作上具有操作名称+ Http谓词属性

  1. Register a single route in Global.asax using a specified action parameter. 使用指定的action参数在Global.asax注册单个路由。

     public override void RegisterArea(AreaRegistrationContext context) { // actions should handle: GET, POST, PUT, DELETE context.MapRoute("Api-SinglePost", "api/posts/{id}", new { controller = "Posts", action = "SinglePost" }); } 
  2. Apply both ActionName and HttpVerb attributes to controller actions ActionNameHttpVerb属性应用于控制器操作

     [HttpGet] [ActionName("SinglePost")] public JsonResult Get(string id) { return Json(_service.Get(id)); } [HttpDelete] [ActionName("SinglePost")] public JsonResult Delete(string id) { return Json(_service.Delete(id)); } [HttpPost] [ActionName("SinglePost")] public JsonResult Create(Post post) { return Json(_service.Save(post)); } [HttpPut] [ActionName("SinglePost")] public JsonResult Update(Post post) { return Json(_service.Update(post);); } 

Method 2: Unique Routes + Verb Constraints, with Http Verb Attribute on Controller Actions 方法2:唯一路由+动词约束,在控制器动作上具有Http动词属性

  1. Register unique routes in Global.asax with HttpMethodContraint 使用HttpMethodContraintGlobal.asax注册唯一的路由

     var postsUrl = "api/posts"; routes.MapRoute("posts-get", postsUrl + "/{id}", new { controller = "Posts", action = "Get", new { httpMethod = new HttpMethodConstraint("GET") }); routes.MapRoute("posts-create", postsUrl, new { controller = "Posts", action = "Create", new { httpMethod = new HttpMethodConstraint("POST") }); routes.MapRoute("posts-update", postsUrl, new { controller = "Posts", action = "Update", new { httpMethod = new HttpMethodConstraint("PUT") }); routes.MapRoute("posts-delete", postsUrl + "/{id}", new { controller = "Posts", action = "Delete", new { httpMethod = new HttpMethodConstraint("DELETE") }); 
  2. Use only an Http Verb Attribute on the Controller Actions 在Controller Actions上仅使用Http Verb属性

     [HttpGet] public JsonResult Get(string id) { return Json(_service.Get(id)); } [HttpDelete] public JsonResult Delete(string id) { return Json(_service.Delete(id)); } [HttpPost] public JsonResult Create(Post post) { return Json(_service.Save(post)); } [HttpPut] public JsonResult Update(Post post) { return Json(_service.Update(post);); } 

Both of these methods let me have uniquely named Controller Action Methods, and allow for RESTful routes tied to the verbs... but what is inherently different about restricting the route vs. using a proxy action name? 这两种方法都让我拥有唯一的命名控制器操作方法,并允许与动词绑定的RESTful路由...... 但是限制路由与使用代理操作名称本质上有什么不同?

You won't get an authoritative answer by here are my 2 cents: 你不会得到一个权威的答案,这是我的2美分:

I prefer method 2 because then you have all your routing in one place. 我更喜欢方法2,因为那时你将所有路由都放在一个地方。 You can encapsulate your routing into a method eg MapResourceRoutes(string controller, string uri) and have it used muptiple times throughout your API. 您可以将路由封装到方法中,例如MapResourceRoutes(string controller, string uri)并在整个API中使用多次。

Also method 2 gives you clearly named routes that you can use for linking and reverse routing. 方法2还为您提供了可用于链接和反向路由的明确命名的路由。

此时,正确的答案是使用Web API。

I don't know that you will ever find an authoritative answer, but I will offer my opinion and as you can tell by my points, my opinion matters ;-). 我不知道你会找到一个权威的答案,但我会提出我的意见,你可以告诉我的观点,我的意见很重要;-)。 My purist self thinks that the first option is more pure, however my experience has been that helper methods like Url.Action() can sometimes have trouble resolving the correct route with this approach and I have taken to the second method as it really only has implications internally as the api looks identical to the consumer. 我的纯粹自我认为第一个选项更纯粹,但是我的经验是Url.Action()这样的辅助方法有时候很难用这种方法解决正确的路径而且我已经采用了第二种方法,因为它实际上只有内部的影响因为api看起来与消费者相同。

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

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