如何ASP.NET Web.api处理两种方式与名称以获得? [英] How does ASP.NET Web.api handle two methods with names starting with GET?

查看:227
本文介绍了如何ASP.NET Web.api处理两种方式与名称以获得?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看微软的下面的教程。按照本教程中,

I am looking at the following tutorial from Microsoft. As per this tutorial,

在第一个例子中,产品命名控制器匹配
  ProductsController的。该请求是一个GET请求,因此该框架
  将查找的ProductsController的方法,其名称开头
  得到...。此外,该URI不包含可选的{id}二
  段,所以该框架查找不带参数的方法。该
  :: ProductsController的方法GetAllProducts符合所有这些
  要求。

In the first example, "products" matches the controller named ProductsController. The request is a GET request, so the framework looks for a method on ProductsController whose name starts with "Get...". Furthermore, the URI does not contain the optional {id} segment, so the framework looks for a method with no parameters. The ProductsController::GetAllProducts method meets all of these requirements.

如果有两个方法,如GetAllProducts()和GetSoldProducts()会发生什么?两人都没有参数。

What happens if there are two methods like GetAllProducts() and GetSoldProducts()? Both have no parameters.

<一个href=\"http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api\">Your第一个Web API教程

推荐答案

假设你正在使用的缺省路由简单的答案是:你的类的第一个定义的方法(在顶部)将会被调用。另一种方法是不可访问的。

Assuming you're using the default routes the short answer is : the method defined first (at the top) of your class will be called. the other method is inaccessible.

请注意:表现为上述匹配多种方法公测 - 对RC&放大器;发行版本是多一点强迫症。如果有多个潜在匹配它抛出一个错误。这一更改移除多暧昧匹配的混乱。与此同时,它降低了我们在同一个控制器混合REST和RPC风格的界面,依靠订单和放大器的能力;重叠的路线。

NOTE : the beta behaved as above for 'matching multiple methods' - the RC & Release version is a bit more OCD. It throws an error if there are multiple potential matches. This change removes the confusion of multiple ambiguous matches. At the same time, it reduces our ability to mix REST and RPC style interfaces in the same controller, relying on the order & overlapping routes.

从<一个宽松的偷窃href=\"http://stackoverflow.com/questions/10439738/how-does-a-method-in-mvc-webapi-map-to-an-http-verb/10471854#10471854\">another文章中,我在话题写道:

的WebAPI语义匹配

语义使用的WebAPI的匹配相当简单。

The matching semantic used by WebAPI is fairly simple.


  1. 它与动词的动作名称相匹配(动词=得到什么?寻找开头为get方法)

  2. 如果一个参数传递,该API旨在动作与参数

所以你的code样品中不带参数的GET请求获取*()匹配功能没有参数。包含和ID获取查找一个获取***(INT ID)

So in your code sample a GET request without a parameter matches the Get*( ) function without an parameters. A Get containing and ID looks for a Get***(int id).

例子

虽然匹配的语义很简单,它创造了MVC开发一些混乱(嗯,至少这个开发者)。让我们来看一些例子:

While the matching semantic is simple, it creates some confusion for MVC developers (well at least this developer). Lets look at some examples :

奇名称的 - 因为它以get开头的GET方法可以命名为任何东西,这么久。因此,在小部件控制器的情况下,你可以命名你的函数 GetStrawberry(),它仍然会匹配。匹配看作是这样的: methodname.StartsWith(GET)

Odd Names - Your get method can be named anything, so long as it starts with "get". So in the case of a widget controller you can name your functions GetStrawberry() and it will still be matched. Think of the matching as something like : methodname.StartsWith("Get")

多个匹配的方法的 - 如果你有一个不带参数的两个Get方法会发生什么? GetStrawberry() GetOrange()。尽我可以告诉,首先定义(该文件的顶部)在code中的功能赢得...奇怪。这在你的控制器无法访问(至少默认路由)使得一些方法的副作用....陌生人。

Multiple Matching Methods - What happens if you have two Get methods with no parameters? GetStrawberry() and GetOrange(). As best I can tell, the function defined first (top of the file) in your code wins ...strange. This has the side effect of making some methods in your controller unreachable (at least with the default routes)....stranger.

更新

@WinFXGuy - 这是一个有点长加入了注释,但...

@WinFXGuy - This was a bit long to put in a comment, but ...

不要妄下结论!我试着回答您提出的问题,但是这只是故事的一半。有很多可以做,以改变默认的行为。

Don't jump to conclusions! I tried to answer the question you posed, but that's only half the story. There is plenty you can do to change the default behavior.

第一的WebAPI支持大部分的OData 规范的。如果你泡了的IQueryable 到你的控制器,OData的PARAMATERS会自动与查询对象集成。它需要的参数,如 $过滤器 $顶部 $跳过。所以,你在你的情况下,你可以写一个方法,并传递类似 $ =过滤SALE_DATE NEQ空

First, WebAPI supports much of the oData spec. If you bubble an IQueryable up to your controller, oData paramaters are automatically integrated with the query object. It takes parameters like $filter, $top, and $skip. So you in your case you can write one method and pass something like $filter=sale_date neq null.

此外,您可以将 [RESULTLIMIT] 属性为prevent人问15十亿条记录。

Additionally, you can apply the [ResultLimit] attribute to prevent people asking for 15 billion records.

您可以修改路线。缺省路由走向一个RESTful API,你一般有每个实体1控制目标。您可以更改路线,使之RPC样式。

Second you can modify the routes. The default routes aim towards a RESTful api, where you generally have 1 controller per entity. You can change the routes and make it RPC style.

如果你看看我的链接后​​我解释我是如何保持默认路由的结合,增加了子文件夹,也让更多的方法调用地方,我需要 GetAllProducts情景() GetSoldProducts()

If you look at my linked post I explain how I kept the default route binding, added 'sub folders' and also allowed additional method calls for scenarios where i needed GetAllProducts() and GetSoldProducts().

这篇关于如何ASP.NET Web.api处理两种方式与名称以获得?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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