Export (0) Print
Expand All

RouteCollection Class

Provides a collection of routes for ASP.NET routing.

Namespace:  System.Web.Routing
Assembly:  System.Web.Routing (in System.Web.Routing.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class RouteCollection : Collection<RouteBase>

The RouteCollection class provides methods that enable you to manage a collection of objects that derive from the RouteBase class.

Typically, you will use the static Routes property of the RouteTable class to retrieve a RouteCollection object. The Routes property stores all the routes for an ASP.NET application. ASP.NET routing iterates through the routes in the Routes property to find the route that matches a URL.

To construct a URL, you call the GetVirtualPath method and pass in a collection of values. The GetVirtualPath method finds the first route with parameters that match the values that you passed in, and returns a VirtualPathData object that contains information about the matching route. You retrieve the URL through the VirtualPath property of the VirtualPathData object.

You can add a route either with a name or without a name. Including a name enables you to distinguish between similar routes when URLs are constructed. If you do not specify a name, ASP.NET routing uses the first matching route in the collection to construct a URL.

When you add an unnamed route to the RouteCollection object, you cannot add a route that already is in the collection. When you add a named route, you cannot use a name that already identifies a route in the collection.

You use the GetReadLock method and the GetWriteLock method to make sure that you interact with the collection without conflicts from other processes.

The following example shows how to add a route to a RouteCollection object.

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()
    ));
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2014 Microsoft