【问题标题】:MVC3 REST Routes & Http VerbsMVC3 REST 路由和 Http 动词
【发布时间】:2012-01-04 02:35:49
【问题描述】:

由于我的previous question,我发现了两种在 MVC3 中处理 REST 路由的方法。

这是一个后续问题,我试图了解这两种方法之间的事实差异/微妙之处。如果可能的话,我正在寻找权威的答案。

方法一:单路由,控制器动作上带有动作名称+Http动词属性

  1. 使用指定的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. 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););
    }
    

方法2:唯一路由+动词约束,控制器动作上带有Http动词属性

  1. 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. 在控制器动作上仅使用 Http 动词属性

    [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););
    }
    

这两种方法都让我拥有唯一命名的控制器操作方法,并允许绑定到动词的 RESTful 路由...但是限制路由与使用代理操作名称有什么本质上的不同? em>

【问题讨论】:

    标签: c# asp.net-mvc http rest routing


    【解决方案1】:

    这是我的 2 美分,你不会得到权威的答案:

    我更喜欢方法 2,因为这样您就可以将所有路由集中在一个地方。您可以将您的路由封装到一个方法中,例如MapResourceRoutes(string controller, string uri) 并在整个 API 中多次使用它。

    方法 2 还为您提供了明确命名的路由,可用于链接和反向路由。

    【讨论】:

      【解决方案2】:

      我不知道您是否会找到权威的答案,但我会提出我的意见,正如您可以从我的观点中看出的那样,我的意见很重要 ;-)。我的纯粹主义者认为第一个选项更纯粹,但是我的经验是,像 Url.Action() 这样的辅助方法有时可能无法使用这种方法解决正确的路线,我已经采用了第二种方法,因为它真的只有内部含义,因为 api 看起来与使用者相同。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2012-01-26
          • 2012-05-16
          • 2011-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多