Export (0) Print
Expand All

Route Class

Provides properties and methods for defining a route and for obtaining information about the route.

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

'Declaration
Public Class Route _
	Inherits RouteBase

The Route type exposes the following members.

  NameDescription
Public methodRoute(String, IRouteHandler)Initializes a new instance of the Route class, by using the specified URL pattern and handler class.
Public methodRoute(String, RouteValueDictionary, IRouteHandler)Initializes a new instance of the Route class, by using the specified URL pattern, default parameter values, and handler class.
Public methodRoute(String, RouteValueDictionary, RouteValueDictionary, IRouteHandler)Initializes a new instance of the Route class, by using the specified URL pattern, default parameter values, constraints, and handler class.
Public methodRoute(String, RouteValueDictionary, RouteValueDictionary, RouteValueDictionary, IRouteHandler)Initializes a new instance of the Route class, by using the specified URL pattern, default parameter values, constraints, custom values, and handler class.
Top

  NameDescription
Public propertyConstraintsGets or sets a dictionary of expressions that specify valid values for a URL parameter.
Public propertyDataTokensGets or sets custom values that are passed to the route handler, but which are not used to determine whether the route matches a URL pattern.
Public propertyDefaultsGets or sets the values to use if the URL does not contain all the parameters.
Public propertyRouteExistingFilesGets or sets a value that indicates whether ASP.NET routing should handle URLs that match an existing file. (Inherited from RouteBase.)
Public propertyRouteHandlerGets or sets the object that processes requests for the route.
Public propertyUrlGets or sets the URL pattern for the route.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetRouteDataReturns information about the requested route. (Overrides RouteBase.GetRouteData(HttpContextBase).)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetVirtualPathReturns information about the URL that is associated with the route. (Overrides RouteBase.GetVirtualPath(RequestContext, RouteValueDictionary).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodProcessConstraintDetermines whether a parameter value matches the constraint for that parameter.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The Route class enables you to specify how routing is processed in an ASP.NET application. You create a Route object for each URL pattern that you want to map to a class that can handle requests that correspond to that pattern. You then add the route to the Routes collection. When the application receives a request, ASP.NET routing iterates through the routes in the Routes collection to find the first route that matches the pattern of the URL.

Set the Url property to a URL pattern. The URL pattern consists of segments that come after the application name in an HTTP request. For example, in the URL http://www.contoso.com/products/show/beverages, the pattern applies to products/show/beverages. A pattern with three segments, such as {controller}/{action}/{id}, matches the URL http://www.contoso.com/products/show/beverages. Each segment is delimited by the / character. When a segment is enclosed in braces ( and ), the segment is referred to a URL parameter. ASP.NET routing retrieves the value from the request and assigns it to the URL parameter. In the previous example, the URL parameter action is assigned the value show. If the segment is not enclosed in braces, the value is treated as a literal value.

Set the Defaults property to a RouteValueDictionary object that includes values that are used if a parameter is missing from the URL or to set additional values that are not parameterized in the URL. Set the Constraints property to a RouteValueDictionary object that contains values that are either regular expressions or IRouteConstraint objects. These values are used to determine whether a parameter value is valid.

The following example shows how to create a Route object and add it to the Routes property.

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

.NET Framework

Supported in: 4.5, 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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