RouteTable.Routes RouteTable.Routes RouteTable.Routes RouteTable.Routes Property

定義

取得從 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

屬性值

物件,包含集合中的所有路徑。An object that contains all the routes in the collection.

範例

下列範例示範如何新增Route物件至Routes屬性。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屬性的事件處理常式從Application_StartGlobal.asax 檔案中的事件。Typically, 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.

適用於

另請參閱