RouteTable.Routes 属性

定义

获取从 RouteBase 类派生的对象的集合。Gets a collection of objects that derive from the RouteBase class.

public:
 static property System::Web::Routing::RouteCollection ^ Routes { System::Web::Routing::RouteCollection ^ get(); };
public static System.Web.Routing.RouteCollection Routes { get; }
member this.Routes : System.Web.Routing.RouteCollection
Public Shared ReadOnly Property Routes As RouteCollection

属性值

RouteCollection

包含集合中所有路由的对象。An object that contains all the routes in the collection.

示例

下面的示例演示如何将对象添加 RouteRoutes 属性。The following example shows how to add a Route object to the Routes property.

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new Route
    (
         "Category/{action}/{categoryName}"
         , new CategoryRouteHandler()
    ));
}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub

Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    Dim urlPattern As String
    Dim categoryRoute As Route
    
    urlPattern = "Category/{action}/{categoryName}"
    
    categoryRoute = New Route(urlPattern, New CategoryRouteHandler)
    
    routes.Add(categoryRoute)
End Sub

注解

路由是用于处理请求并可用于动态构造 Url 的 URL 模式。Routes are URL patterns that are used for processing requests and that can be used to construct URLs dynamically. Routes属性是 static Shared Visual Basic) 中 (的属性,表示用于指定如何将 URL 请求与处理请求的类匹配的所有对象。The Routes property is a static property (Shared in Visual Basic) that represents all the objects that are used to specify how a URL request is matched to a class that handles the request. 若要指定路由,请将路由定义添加到 Routes 属性。To specify a route, you add the route definition to the Routes property. 通常,会在 Routes global.asax 文件中的事件的事件处理程序中添加到属性的路由 Application_StartTypically, you add routes to the Routes property from an event handler for the Application_Start event in the Global.asax file.

当 ASP.NET 应用程序处理请求时,应用程序将循环访问属性中的路由集合, Routes 以查找与 URL 请求的格式匹配的路由。When an ASP.NET application handles a request, the application iterates through the collection of routes in the Routes property to find the route that matches the format of the URL request. 添加到属性中的路由顺序 Routes 非常重要,因为应用程序使用集合中与 URL 匹配的第一个路由。The order of the routes that you add to the Routes property is significant, because the application uses the first route in the collection that matches the URL.

适用于