当前位置 > 首页 > Asp.net

【ASP.NET Web API教程】4.2 路由与动作选择

2012-12-4 12:50:00来源:Asp.net

注:本文是【ASP.NET Web API系列教程】的一部分,如果您是第一次看本系列教程,请先看前面的内容。

4.2 Routing and Action Selection
4.2 路由与动作选择

本文引自: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:
路由有三个主要阶段:

  1. Matching the URI to a route template.
    将URI匹配到一个路由模板。
  2. Selecting a controller.
    选择一个控制器
  3. Selecting an action.
    选择一个动作。

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.
你可以用自己的自定义行为来替换这一过程的某些部分。在本文中,我会描述默认行为。最后,我会注明可以在什么地方自定义行为。

Route Templates
路由模板

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}”。

  • "{controller}" provides the name of the controller.
    “{controller}”提供控制器名。
  • "{action}" provides the name of the action. In Web API, the usual convention is to omit "{action}".
    “{action}”提供动作名。在Web API中,通常的约定是忽略“{action}”的。

Defaults
默认值

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”。

Route Dictionary
路由字典

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”,路由字典将含有:

  • controller: "products"
  • category: "all"

由于这条URI路径中不包含id,因此,id的值将采用默认的RouteParameter.Optional,所以,路由字典中不会包含id片段的键值对 — 译者注

For "api/products/toys/123", however, the route dictionary will contain:
然而,对于“api/products/toys/123”,路由字典将含有:

  • controller: "products"
  • category: "toys"
  • id: "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:
默认值也可以包含未出现在路由模板中的值。若这条路由匹配,则该值会被存储在路由字典中。例如: