注:本文是【ASP.NET Web API系列教程】的一部分,如果您是第一次看本系列教程,请先看前面的内容。
本文引自:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection
By Mike Wasson | July 27, 2012
作者:Mike Wasson | 日期:2012-7-27
This article describes how ASP.NET Web API routes an HTTP request to a particular action on a controller.
本文描述ASP.NET Web API如何把一个HTTP请求路由到控制器的一个特定的方法上。
For a high-level overview of routing, see Routing in ASP.NET Web API.
关于路由的总体概述,参阅“ASP.NET Web API中的路由”(本系列教程的前一小节 — 译者注)。
This article looks at the details of the routing process. If you create a Web API project and find that some requests don’t get routed the way you expect, hopefully this article will help.
本文考察路由过程的细节。如果你创建了一个Web API项目,并发现有些请求并未按你期望的方式被路由,希望这篇文章对你会有所帮助。
Routing has three main phases:
路由有三个主要阶段:
You can replace some parts of the process with your own custom behaviors. In this article, I describe the default behavior. At the end, I note the places where you can customize the behavior.
你可以用自己的自定义行为来替换这一过程的某些部分。在本文中,我会描述默认行为。最后,我会注明可以在什么地方自定义行为。
A route template looks similar to a URI path, but it can have placeholder values, indicated with curly braces:
路由模板看上去类似于一个URI路径,但它可以具有占位符,这是用花括号来指示的:
"api/{controller}/public/{category}/{id}"
When you create a route, you can provide default values for some or all of the placeholders:
当创建一条路由时,可以为某些或所有占位符提供默认值:
defaults: new { category = "all" }
You can also provide constraints, which restrict how a URI segment can match a placeholder:
也可以提供约束,它限制URI片段如何与占位符匹配:
constraints: new { id = @"\d+" } // Only matches if "id" is one or more digits. // 用正则表达式限制片段的取值,上语句表明,id片段的值必须是一个或多个数字。 // 因此,URI中id片段必须是数字才能与这条路由匹配
The framework tries to match the segments in the URI path to the template. Literals in the template must match exactly. A placeholder matches any value, unless you specify constraints. The framework does not match other parts of the URI, such as the host name or the query parameters. The framework selects the first route in the route table that matches the URI.
框架会试图把URI路径的片段与该模板进行匹配。模板中的文字必须严格匹配。占位符可以匹配任意值,除非你指定了约束。框架不会匹配URI的其它部分,如主机名或查询字符串。框架会选择路由表中与URI匹配的第一条路由。
There are two special placeholders: "{controller}" and "{action}".
有两个特殊的占位符:“{controller}”和“{action}”。
If you provide defaults, the route will match a URI that is missing those segments. For example:
如果提供默认值,该路由将能够匹配缺少这些片段的URI。例如:
routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{category}", defaults: new { category = "all" } );
The URI "http://localhost/api/products" matches this route. The "{category}" segment is assigned the default value "all".
URI“http://localhost/api/products”与这条路由是匹配的。“{category}”片段被赋成了默认值“all”。
If the framework finds a match for a URI, it creates a dictionary that contains the value for each placeholder. The keys are the placeholder names, not including the curly braces. The values are taken from the URI path or from the defaults. The dictionary is stored in the IHttpRouteData object.
如果框架为一个URI找到一个匹配,它会创建一个字典,其中包含了每个占位符的值。(字典的内容是一些“键-值”对 — 译者注)。其键是不带花括号的占位符名称。其值取自URI路径或默认值。该字典被存储在IHttpRouteData对象中。
During this route-matching phase, the special "{controller}" and "{action}" placeholders are treated just like the other placeholders. They are simply stored in the dictionary with the other values.
在路由匹配阶段,“{controller}”和“{action}”占位符的处理与其它占位符的处理是一样的。只是把它们简单地用值存储在字典中。
A default can have the special value RouteParameter.Optional. If a placeholder gets assigned this value, the value is not added to the route dictionary. For example:
在默认值中可以使用特殊的RouteParameter.Optional值。如果一个占位符被赋予了这个值,则该值不会被添加到路由字典。例如:
routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{category}/{id}", defaults: new { category = "all", id = RouteParameter.Optional } );
For the URI path "api/products", the route dictionary will contain:
对于URI路径“api/products”,路由字典将含有:
由于这条URI路径中不包含id,因此,id的值将采用默认的RouteParameter.Optional,所以,路由字典中不会包含id片段的键值对 — 译者注
For "api/products/toys/123", however, the route dictionary will contain:
然而,对于“api/products/toys/123”,路由字典将含有:
The defaults can also include a value that does not appear anywhere in the route template. If the route matches, that value is stored in the dictionary. For example:
默认值也可以包含未出现在路由模板中的值。若这条路由匹配,则该值会被存储在路由字典中。例如:
routes.MapHttpRoute( name: "Root", routeTemplate: "api/root/{id}", defaults: new { controller = "customers", id = RouteParameter.Optional } );
If the URI path is "api/root/8", the dictionary will contain two values:
如果URI路径是“api/root/8”,字典将含有两个值:
Controller selection is handled by the IHttpControllerSelector.SelectController method. This method takes an HttpRequestMessage instance and returns an HttpControllerDescriptor. The default implementation is provided by the DefaultHttpControllerSelector class. This class uses a straightforward algorithm:
控制器选择是由IHttpControllerSelector.SelectController方法来处理的。这个方法以HttpRequestMessage实例为参数,并返回HttpControllerDescriptor。其默认实现是由DefaultHttpControllerSelector类提供的。这个类使用了一种很直接的算法:
For example, if the route dictionary contains the key-value pair "controller" = "products", then the controller type is "ProductsController". If there is no matching type, or multiple matches, the framework returns an error to the client.
例如,如果路由字典的键-值对为“controller”=“products”,那么,控制器类型便为“ProductsController”。如果没有匹配类型,或有多个匹配,框架会给客户端返回一条错误。
For step 3, DefaultHttpControllerSelector uses the IHttpControllerTypeResolver interface to get the list of Web API controller types. The default implementation of IHttpControllerTypeResolver returns all public classes that (a) implement IHttpController, (b) are not abstract, and (c) have a name that ends in "Controller".
对于步骤3,DefaultHttpControllerSelector使用IHttpControllerTypeResolver接口以获得Web API控制器类型的列表。IHttpControllerTypeResolver的默认实现会返回所有符合以下条件的public类:(a)实现IHttpController的类(b)是非抽象类,且(c)名称以“Controller”结尾的类。
After selecting the controller, the framework selects the action by calling the IHttpActionSelector.SelectAction method. This method takes an HttpControllerContext and returns an HttpActionDescriptor.
选择了控制器之后,框架会通过调用IHttpActionSelector.SelectAction方法来选择动作。这个方法以HttpControllerContext为参数,并返回HttpActionDescriptor。
The default implementation is provided by the ApiControllerActionSelector class. To select an action, it looks at the following:
默认实现是由ApiControllerActionSelector类提供的。为了选择一个动作,会查找以下方面:
Before looking at the selection algorithm, we need to understand some things about controller actions.
在查找选择算法之前,我们需要理解控制器动作的一些事情。
Which methods on the controller are considered "actions"? When selecting an action, the framework only looks at public instance methods on the controller. Also, it excludes "special name" methods (constructors, events, operator overloads, and so forth), and methods inherited from the ApiController class.
控制器中的哪些方法被看成为是“动作”?当选择一个动作时,框架只考察控制器的public实例方法。而且,它会排除“special name"特殊名称”的方法(构造器、事件、操作符重载等等),以及继承于ApiController类的方法。
这里按原文的含义似乎是要排除API控制器中的public方法,但译者认为,框架会把API控制器中的public方法看成是动作 — 译者注
HTTP Methods. The framework only chooses actions that match the HTTP method of the request, determined as follows:
HTTP方法。框架只会选择与请求的HTTP方法匹配的动作,确定如下:
Parameter Bindings. A parameter binding is how Web API creates a value for a parameter. Here is the default rule for parameter binding:
参数绑定。参数绑定是指Web API如何创建参数值。以下是参数绑定的默认规则:
Simple types include all of the .NET Framework primitive types, plus DateTime, Decimal, Guid, String, and TimeSpan. For each action, at most one parameter can read the request body.
简单类型包括所有“.NET 框架简单类型”,另外还有,DateTime、Decimal、Guid、String和TimeSpan。对于每一个动作,最多只有一个参数可以读取请求体。
It is possible to override the default binding rules. See WebAPI Parameter binding under the hood.
也可以重写这种默认的绑定规则。参见WebAPI Parameter binding under the hood(作者的一篇博客文章 — 译者注)。
With that background, here is the action selection algorithm.
在这种背景下,动作选择算法如下:
Step #3 is probably the most confusing. The basic idea is that a parameter can get its value either from the URI, from the request body, or from a custom binding. For parameters that come from the URI, we want to ensure that the URI actually contains a value for that parameter, either in the path (via the route dictionary) or in the query string.
第3步可能会让人困扰。其基本思想是,可以从URI、或请求体、或一个自定义绑定来获取参数值(这里指出了方法参数的来源 — 译者注)。对于来自URI的参数,我们希望确保URI在其路径(通过路由字典)或查询字符串中实际包含了一个用于此参数的值。
For example, consider the following action:
例如,考虑以下动作:
public void Get(int id)
The id parameter binds to the URI. Therefore, this action can only match a URI that contains a value for "id", either in the route dictionary or in the query string.
其id参数绑定到URI。因此,这个动作只能匹配在路由字典或查询字符串中包含了“id”值的URI。
Optional parameters are an exception, because they are optional. For an optional parameter, it's OK if the binding can't get the value from the URI.
可选参数是一个例外,因为它们是可选的。对于可选参数,如果绑定不能通过URI获取它的值,是没关系的。
Complex types are an exception for a different reason. A complex type can only bind to the URI through a custom binding. But in that case, the framework cannot know in advance whether the parameter would bind to a particular URI. To find out, it would need to invoke the binding. The goal of the selection algorithm is to select an action from the static description, before invoking any bindings. Therefore, complex types are excluded from the matching algorithm.
复合类型是另一种原因的例外。复合类型只能通过自定义绑定来绑定到URI。但在这种情况下,框架不能预知该参数是否会绑定到一个特定的URI。为了找出来,框架需要调用绑定。选择算法的目的是在调用绑定之前根据静态描述来选择一个动作。因此,复合类型是属于匹配算法之外的。
After the action is selected, all parameter bindings are invoked.
动作选择之后,会调用所有参数绑定。
Summary:
小结:
Routes:
路由
routes.MapHttpRoute( name: "ApiRoot", routeTemplate: "api/root/{id}", defaults: new { controller = "products", id = RouteParameter.Optional } ); routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );
Controller:
控制器:
public class ProductsController : ApiController { public IEnumerable<Product> GetAll() {} public Product GetById(int id, double version = 1.0) {} [HttpGet] public void FindProductsByName(string name) {} public void Post(Product value) {} public void Put(int id, Product value) {} }
HTTP request:
HTTP请求:
GET http://localhost:34701/api/products/1?version=1.5&details=1
The URI matches the route named "DefaultApi". The route dictionary contains the following entries:
该URI与名为“DefaultApi”路由匹配。路由字典包含以下条目:
The route dictionary does not contain the query string parameters, "version" and "details", but these will still be considered during action selection.
该路由字典并未包含查询字符串参数“version”和“details”,但这些将在动作选择期间考虑。
From the "controller" entry in the route dictionary, the controller type is ProductsController.
根据路由字典中的“controller”条目,控制器类型是ProductsController。
The HTTP request is a GET request. The controller actions that support GET are GetAll, GetById, and FindProductsByName. The route dictionary does not contain an entry for "action", so we don’t need to match the action name.
该HTTP请求是一个GET请求。支持GET的控制器动作是GetAll、GetById和FindProductsByName。路由字典不包含“action”条目,因此不需要匹配动作名。
Next, we try to match parameter names for the actions, looking only at the GET actions.
下一步,会试图匹配这些动作的参数名,只考查GET动作。
Action 动作 |
Parameters to Match 要匹配的参数 |
---|---|
GetAll | None(无) |
GetById | "id" |
FindProductsByName | "name" |
Notice that the version parameter of GetById is not considered, because it is an optional parameter.
注意,不会考虑GetById的version参数,因为它是一个可选参数。
The GetAll method matches trivially. The GetById method also matches, because the route dictionary contains "id". The FindProductsByName method does not match.
GetAll方法非常匹配(这是最佳匹配,但不是最终选择 — 译者注)。GetById方法也匹配,因为路由字典包含了“id”。FindProductsByName方法不匹配。
The GetById method wins, because it matches one parameter, versus no parameters for GetAll. The method is invoked with the following parameter values:
GetById方法是赢家,因为它匹配了一个参数,而GetAll无参数。该方法将以以下参数值被调用:
Notice that even though version was not used in the selection algorithm, the value of the parameter comes from the URI query string.
注意,虽然version未被用于选择算法,但该参数值会取自URI查询字符串。
Web API provides extension points for some parts of the routing process.
Web API为路由过程的某些部分提供了扩展点。
Interface 接口 |
Description 描述 |
---|---|
IHttpControllerSelector | Selects the controller. 选择控制器。 |
IHttpControllerTypeResolver | Gets the list of controller types. The DefaultHttpControllerSelector chooses the controller type from this list. 获取控制器类型列表。DefaultHttpControllerSelector从该列表选择控制器。 |
IAssembliesResolver | Gets the list of project assemblies. The IHttpControllerTypeResolver interface uses this list to find the controller types. 获取项目程序集列表。IHttpControllerTypeResolver接口用该列表查找控制器类型。 |
IHttpControllerActivator | Creates new controller instances. 创建控制器新实例。 |
IHttpActionSelector | Selects the action. 选择动作。 |
IHttpActionInvoker | Invokes the action. 调用动作。 |
To provide your own implementation for any of these interfaces, use the Services collection on the HttpConfiguration object:
要为以上任一接口提供自己的实现,可使用HttpConfiguration对象的Services集合:
var config = GlobalConfiguration.Configuration; config.Services.Replace(typeof(IHttpControllerSelector), new MyControllerSelector(config));
看完此文如果觉得有所收获,请给个推荐