路线MVC 4的Web API的? [英] Routes for MVC 4 Web APIs?

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

问题描述

在我创造一个MVC4的Web API的初始刺伤。我注意到有关路线的处理方式,这似乎有点过了几件事情:


  • 控制器不再曾经有4个以上的行为?

  • 只支持一个HTTP动词仍然得到它的资源是非常自己的控制器?

  • 路由层次往往是非常平坦的,或者

  • 必须指定一个非常大的数量的路由/相比,MVC应用对人类保持不变。

我已经创建了一个仅使用两个或三个途径相当大的网站,但我刚开始我的API和我到几乎一打。我猜其性质的API借给自己不是网站的网址更深,但是这似乎有点过分了。

我觉得我缺少什么地方命名空间或路由约定。我属性几乎所有的这一个事实,即操作名称是路线不再一部分,只是HTTP方法。是否有比参数匹配,控制器名称和HTTP方法?

之外的任何支持的路由方式
解决方案

您可以修改Global.asax中路由。默认设置为:

  routes.MapHttpRoute(
            名称:DefaultApi
            routeTemplate:API / {}控制器/(编号),
            默认:新{ID = RouteParameter.Optional}

但你可以改变它,例如使用操作名称:

  routes.MapHttpRoute(
名称:ActionApi
routeTemplate:API / {控制器} / {行动} / {ID}
默认:新{ID = RouteParameter.Optional}

有关这对MVC的Web API的外观自定义路由信息的文章

基于注释的更新

以上是问题的答案,我可以改变路由?简短的回答是肯定的,但在另一种答案是指出你可能不希望并保持良好的REST API。 MVC的Web API与它默认的路由认为你是对资源工作的基本概念REST,与控制器重新presenting资源。在您的其他问题你给的例子:

 删除/帐号/ 1234 / NOTE / 321<  - 帐户删除1234 321注

如果你想在1234的帐户删除便笺在这种情况下,你正在使用的资源是一个注释。这方面的一个更好的再presentation是使用查询字符串,所以你的这个例子API应该是这样的:

<?pre> 删除/ NOTE / 321 =占到1234 LT; - 帐户删除1234 321注

这样,您就不必与路由很乱,很显然,正在采取行动资源在与过滤器的说明被使用。但我也会质疑是否增加这个明确的过滤器(即WHERE子句)是必要的,如果用于注意id是整个应用程序唯一的,不只是一个特定的帐户。

In my initial stab at creating a MVC4 Web API. I've noticed a few things about the way routes are handled that seems a little off:

  • Controllers no longer ever have more than 4 actions?
  • A resource that only supports one HTTP verb still gets it's very own controller?
  • The route hierarchy tends to be very flat, OR
  • An extremely large number of routes must be specified/maintained compared to MVC apps for humans.

I've created fairly large sites that use only two or three routes, but I'm just starting on my API and I'm up to almost a dozen. I guess APIs by their nature lend themselves to deeper URLs than websites, but this seems a little excessive.

I feel like I'm missing a namespace somewhere or a routing convention. I attribute nearly all of this to the fact that action names are no longer part of the route, just the HTTP method. Is there a way to support routes by anything other than parameter matching, controller name, and HTTP method?

解决方案

You can modify the routing in Global.asax. The default is set to:

           routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }

But you could change it for example to use the action name:

routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }

For more information on custom routing in MVC WEb API look at this article.

Update based on comments

Above was the answer to the question, "can I change the routing?" The short answer is yes, but as was pointed out in another answer you may not want to and maintain a good REST API. MVC Web API with it default routing maintains the basic REST concept that you are working on resources, with the controller representing the resource. In your other question you give the example:

DELETE /account/1234/note/321 <- delete note 321 on account 1234

Where you want to delete a note on account 1234. In this case the resource you are working on is a note. A better representation for this would be to use query string so your API for this example would look like this:

DELETE /note/321?account=1234 <- delete note 321 on account 1234

This way you do not need to mess with the routing and it is clear that the resource being acted upon is a note with filters being used. But I would also question whether adding this explicit filter (i.e. where clause) is necessary if the id for note is unique for the whole application and not just for a specific account.

这篇关于路线MVC 4的Web API的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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