This documentation is archived and is not being maintained.

RouteCollection.Add Method (String, RouteBase)

Adds a route to the end of the RouteCollection object and assigns the specified name to the route.

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

'Declaration
Public Sub Add ( _
	name As String, _
	item As RouteBase _
)
'Usage
Dim instance As RouteCollection 
Dim name As String 
Dim item As RouteBase

instance.Add(name, item)

Parameters

name
Type: System.String

The value that identifies the route. The value can be Nothing or an empty string.

item
Type: System.Web.Routing.RouteBase

The route to add to the end of the collection.

ExceptionCondition
ArgumentNullException

item is Nothing.

ArgumentException

name is already used in the collection.

The Add method enables you to define a name for a route when you add it to the route collection. By defining a name for the route, you can specify a particular route to use when you use routing to construct a URL. Specifying a particular route is important when more than one route matches the values that you pass to the GetVirtualPath method. If you do not specify a named route, ASP.NET routing will use the first route in the collection that matches the values. For more information, see How to: Construct a URL from a Route.

A name can be used only one time in the RouteCollection object.

If you do not have to specify a name for the route, you can add the route by calling the Add method.

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 and assign a name to the route.

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub 

Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    Dim salesUrlPattern As String 
    Dim expensesUrlPattern As String 
    Dim salesRoute As Route
    Dim expensesRoute As Route

    salesUrlPattern = "SalesReport/{year}/{month}"
    salesRoute = New Route(salesUrlPattern, New SalesRouteHandler)
    routes.Add("SalesRoute", salesRoute)

    expensesUrlPattern = "ExpensesReport/{year}/{month}"
    expensesRoute = New Route(expensesUrlPattern, New ExpensesRouteHandler)
    routes.Add("ExpensesRoute", expensesRoute)
End Sub

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
Show: