HTML5技术

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

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

If you want to add custom routes in an MVC application, you use the MapRoute(RouteCollection, String, String) method instead of the MapPageRoute(String, String, String) method. 译文:如果您想在MVC应

If you want to add custom routes in an MVC application, you use the MapRoute(RouteCollection, String, String) method instead of the MapPageRoute(String, String, String) method.
译文:如果您想在MVC应用程序中添加自定义路由,您可以使用MapRoute(RouteCollection、String、String)方法,而不是MapPageRoute(字符串、字符串、字符串)方法。

The following example shows the code that creates default MVC routes in the Global.asax file, as defined in the Visual Studio project template for MVC applications.

 译文:下面的例子展示了在全局中创建默认MVC路由的代码。asax文件,正如在Visual Studio项目模板中定义的MVC应用程序。

public class MvcApplication : System.Web.HttpApplication { RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(); routes.MapRoute( , , { controller = , action = , id = "" } // Parameter defaults ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } }

2.7 Setting Default Values for URL Parameters(为URL参数设置默认值)

When you define a route, you can assign a default value for a parameter. The default value is used if a value for that parameter is not included in the URL. You set default values for a route by assigning a dictionary object to the Defaults property of the Route class. The following example shows how to add a route that has default values, by using the MapPageRoute(String, String, String, Boolean, RouteValueDictionary) method.

译文:当您定义一个路由时,您可以为一个参数指定一个默认值。如果该参数的值没有包含在URL中,则使用默认值。通过将dictionary对象分配给route类的默认属性,可以为路由设置默认值。下面的例子展示了如何通过使用MapPageRoute(字符串、字符串、字符串、布尔值、RouteValueDictionary)方法添加具有默认值的路由。

void Application_Start(object sender, EventArgs e) { RegisterRoutes(RouteTable.Routes); } RegisterRoutes(RouteCollection routes) { routes.MapPageRoute("", , , true, new RouteValueDictionary {{, }, {, }}); }

When ASP.NET routing handles a URL request, the route definition shown in the example (with default values of food for categoryName and showfor action) produces the results that are listed in the following table.

For MVC applications, overloads of the RouteCollectionExtensions.MapRoute method, such as MapRoute(RouteCollection, String, String, Object, Object), enable you to specify defaults.

2.8 Handling a Variable Number of Segments in a URL Pattern(在URL模式中处理可变数量的段)

Sometimes you have to handle URL requests that contain a variable number of URL segments. When you define a route, you can specify that if a URL has more segments than there are in the pattern, the extra segments are considered to be part of the last segment. To handle additional segments in this manner you mark the last parameter with an asterisk (*). This is referred to as a catch-all parameter. A route with a catch-all parameter will also match URLs that do not contain any values for the last parameter. The following example shows a route pattern that matches an unknown number of segments.

译文:有时您必须处理包含一个可变数量的URL段的URL请求,其中。当您定义一个路由时,您可以指定,如果一个URL的片段比模式中有更多的段,那么额外的段被认为是最后一部分的一部分。要以这种方式处理额外的段,您可以用星号(*)标记最后一个参数。这被称为一个笼统的参数。一个包含所有参数的路由也将匹配不包含最后一个参数的任何值的url。下面的示例展示了一个匹配未知数量的段的路由模式。

/ query / { queryname } { * queryvalues }

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

译文:当ASP.NET路由处理一个URL请求时,示例中所示的路由定义产生了下表中列出的结果。

2.9  Adding Constraints to Routes(为路由添加约束)

In addition to matching a URL request to a route definition by the number of parameters in the URL, you can specify that values in the parameters meet certain constraints. If a URL contains values that are outside the constraints for a route, that route is not used to handle the request. You add constraints to make sure that the URL parameters contain values that will work in your application.

 

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

网友点评
i