HTML5技术

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

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

译文:除了通过URL中参数的数量来匹配URL请求外,还可以指定参数中的值满足特定的约束条件。如果一个URL包含的值超出了路由的约束,那么该路由就不会被用于处理请求。您添加了一些约束,以确保URL参数包含在您的应

译文:除了通过URL中参数的数量来匹配URL请求外,还可以指定参数中的值满足特定的约束条件。如果一个URL包含的值超出了路由的约束,那么该路由就不会被用于处理请求。您添加了一些约束,以确保URL参数包含在您的应用程序中工作的值。
Constraints are defined by using regular expressions or by using objects that implement the IRouteConstraint interface. When you add the route definition to the Routes collection, you add constraints by creating a RouteValueDictionary object that contains the verification test. The key in the dictionary identifies the parameter that the constraint applies to. The value in the dictionary can be either a string that represents a regular expression or an object that implements the IRouteConstraint interface.

译文:约束是通过使用正则表达式或使用实现IRouteConstraint接口的对象来定义的。当将路由定义添加到路由集合时,通过创建一个包含验证测试的RouteValueDictionary对象来添加约束。字典中的键标识约束应用到的参数。字典中的值可以是表示正则表达式的字符串,也可以是实现IRouteConstraint接口的对象。
If you provide a string, routing treats the string as a regular expression and checks whether the parameter value is valid by calling the IsMatch method of the Regex class. The regular expression is always treated as case-insensitive. For more information, see .NET Framework Regular Expressions.

译文:如果您提供一个字符串,那么路由将字符串作为一个正则表达式来处理,并通过调用Regex类的IsMatch方法检查参数值是否有效。正则表达式总是被视为不区分大小写的。要了解更多信息,请参阅。净框架正则表达式。
If you provide an IRouteConstraint object, ASP.NET routing checks whether the parameter value is valid by calling the Match method of the IRouteConstraint object. The Match method returns a Boolean value that indicates whether the parameter value is valid.

译文:如果你提供一个IRouteConstraint对象,ASP.NET 路由通过调用IRouteConstraint对象的匹配方法, 以此来检查参数值是否有效。Match方法返回一个布尔值,该值表示参数值是否有效。
The following example shows how to use the MapPageRoute method to create a route that has constraints that limit what values can be included in the locale and year parameters. (In an MVC application, you would use the MapRoute method.)

译文:下面的示例展示了如何使用MapPageRoute方法创建一条路径,该路由限制了地区和年参数中可以包含的值。(在MVC应用程序中,您将使用map路由方法。)

 

RegisterRoutes(RouteCollection routes) 2 { 3 routes.MapPageRoute("", , , 6 true, 7 new RouteValueDictionary , }, {, }}, 9 new RouteValueDictionary , },{, }} 11 ); 12 }

When routing handles a URL request, the route definition shown in the previous example produces the results that are listed in the following table.

 2.10 Scenarios When Routing Is Not Applied(不应用路由时的场景)

Under some circumstances, ASP.NET routing does not handle a request even when is enabled for the Web site. This section describes some scenarios in which routing does not handle the request.

译文:在某些情况下,即使在Web站点启用时,ASP.NET 路由也不处理请求。本部分描述了一些场景,其中路由不处理请求。

A Physical File is Found that Matches the URL Pattern(找到与URL模式匹配的物理文件)

By default, routing does not handle requests that map to an existing physical file on the Web server. For example, a request for is not handled by routing if a physical file exists at Products/Beverages/Coffee.aspx. Routing does not handle the request even if it matches a defined pattern, such as {controller}/{action}/{id}.

译文:默认情况下,路由不处理映射到Web服务器上现有物理文件的请求。例如,如果存在 Products/Beverages/Coffee.aspx.物理文件,请求 不被路由处理。即使它与已定义的模式相匹配,路由不处理请求,例如控制器/动作/id。
If you want routing to handle all requests, even requests that point to files, you can override the default behavior by setting the RouteExistingFiles property of the RouteCollection object to true. When you set this value to true, all requests that match a defined pattern are handled by routing.

译文:如果您想要路由处理所有请求,甚至指向文件的请求,您可以通过设置RouteCollection对象的属性RouteExistingFiles为true,  以此来覆盖默认值。当您将这个值设置为true时,匹配定义模式的所有请求都由路由处理。

Routing Is Explicitly Disabled for a URL Pattern(为URL模式显式禁用路由)

 

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

网友点评