HTML5技术

【ASP.NET MVC 牛刀小试】 ASP.NET MVC 路由 - Alan_beijing(2)

字号+ 作者:H5之家 来源:H5之家 2017-06-29 11:01 我要评论( )

译文:路由是一种被映射到某个处理程序的URL模式。处理程序可能是一个物理文件,如在WebForms运用程序中的aspx文件。处理程序也可能是一个处理请求的类,如MVC应用程序中的控制器。要定义路由,您可以通过指定URL模

       译文:路由是一种被映射到某个处理程序的URL模式。处理程序可能是一个物理文件,如在WebForms运用程序中的aspx文件。处理程序也可能是一个处理请求的类,如MVC应用程序中的控制器。要定义路由,您可以通过指定URL模式、处理程序和路径的名称来创建路由类的实例。

      You add the route to the application by adding the Route object to the static Routes property of the RouteTable class. The Routes property is a RouteCollection object that stores all the routes for the application.You typically do not have to write code to add routes in an MVC application. Visual Studio project templates for MVC include preconfigured URL routes. These are defined in the MvcApplication class, which is defined in the Global.asax file.

      译文:你可以通过将路由对象添加到RouteTable类的静态路由属性中的方式将路由添加到应用程序中。路由属性是一个为应用程序存储所有路由的路由对象。在MVC应用程序中,您通常不需要编写代码来添加路由。VS项目模板为MVC包含了预先配置的URL路由。这些都是在MvcApplication类中定义的,被定义在Global.asac 文件中。

2.4 URL Patterns(URL模式)

A URL pattern can contain literal values and variable placeholders (referred to as URL parameters). The literals and placeholders are located in segments of the URL which are delimited by the slash (/) character.

译文:URL模式可能包含文字值和变量占位符(称为URL参数)。文字和占位符位于URL的片段中,由斜杠(/)字符分隔。

When a request is made, the URL is parsed into segments and placeholders, and the variable values are provided to the request handler. This process is similar to the way the data in query strings is parsed and passed to the request handler. In both cases variable information is included in the URL and passed to the handler in the form of key-value pairs. For query strings both the keys and the values are in the URL. For routes, the keys are the placeholder names defined in the URL pattern, and only the values are in the URL.

译文:当发出请求时,URL被解析为片段和占位符,且变量值被提供给请求处理程序。这个过程类似于查询字符串中的数据被解析并传递给请求处理程序的方式。在这两种情况下,变量信息都包含在URL中,并以键值对的形式传递给处理程序。对于查询字符串,键和值都在URL中。对于路由,键是URL模式中定义的占位符名称,在URL中仅仅是值。

In a URL pattern, you define placeholders by enclosing them in braces ( { and } ). You can define more than one placeholder in a segment, but they must be separated by a literal value. For example, {language}-{country}/{action} is a valid route pattern. However, {language}{country}/{action} is not a valid pattern, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the language placeholder from the value for the country placeholder.

译文:在URL模式中,通过将它们封装在括号(以及)中来定义占位符。您可以在一个段中定义多个占位符,但是必须用文字值分隔它们。例如,语言-国家/行动是一种有效的路线模式。然而,语言国家/action不是一个有效的模式,因为占位符之间没有文字值或分隔符。因此,路由不能决定将语言占位符的值与国家占位符的值分隔开。

The following table shows valid route patterns and examples of URL requests that match the patterns.

下表显示了有效的路由模式和与模式匹配的URL请求示例。

 Typical URL Patterns in MVC Applications

译文:MVC运用程序中的经典模式

 URL patterns for routes in MVC applications typically include {controller} and {action} placeholders.

译文:在MVC运用程序中,路由URL模式通常包含控制器和动作占位符。

When a request is received, it is routed to the UrlRoutingModule object and then to the MvcHandler HTTP handler. The MvcHandler HTTP handler determines which controller to invoke by adding the suffix "Controller" to the controller value in the URL to determine the type name of the controller that will handle the request. The action value in the URL determines which action method to call.

译文:当接收到一个请求时,它被路由到UrlRoutingModule对象,然后发送到MvcHandler HTTP处理程序。MvcHandler HTTP处理程序通过向URL中的控制器值添加后缀“控制器”来确定要调用哪个控制器,以确定控制器的类型名称,该控制器将处理请求。URL中的操作值决定调用哪个操作方法。

For example, a URL that includes the URL path /Products is mapped to a controller named ProductsController. The value in the action parameter is the name of the action method that is called. A URL that includes the URL path /Products/show would result in a call to the Showmethod of the ProductsController class.

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Asp.Net WebForm生命周期的详解 - 天使不哭

    Asp.Net WebForm生命周期的详解 - 天使不哭

    2017-06-15 09:00

  • 关于ASP.NET WebForm与ASP.NET MVC的比较 - 天使不哭

    关于ASP.NET WebForm与ASP.NET MVC的比较 - 天使不哭

    2017-06-09 09:02

  • [asp.net mvc 奇淫巧技] 04 - 你真的会用Action的模型绑定吗? - Emrys5

    [asp.net mvc 奇淫巧技] 04 - 你真的会用Action的模型绑定吗? - Emr

    2017-06-02 13:00

  • Amazing ASP.NET Core 2.0 - Savorboard

    Amazing ASP.NET Core 2.0 - Savorboard

    2017-05-25 14:00

网友点评
d