RouteCollection.Add(String, RouteBase) 方法

定义

RouteCollection 对象末尾添加路由,并为该路由分配指定的名称。Adds a route to the end of the RouteCollection object and assigns the specified name to the route.

public:
 void Add(System::String ^ name, System::Web::Routing::RouteBase ^ item);
public void Add (string name, System.Web.Routing.RouteBase item);
override this.Add : string * System.Web.Routing.RouteBase -> unit
Public Sub Add (name As String, item As RouteBase)

参数

name
String

标识该路由的值。The value that identifies the route. 该值可为 null 或空字符串。The value can be null or an empty string.

item
RouteBase

要添加到集合末尾的路由。The route to add to the end of the collection.

例外

itemnullitem is null.

name 已在集合中使用。name is already used in the collection.

示例

下面的示例演示如何将路由添加到 RouteCollection 对象并为该路由分配一个名称。The following example shows how to add a route to a RouteCollection object and assign a name to the route.

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub

注解

使用 Add 方法,可以在将路由添加到路由集合时定义该路由的名称。The Add method enables you to define a name for a route when you add it to the route collection. 通过定义路由的名称,可以指定使用路由构造 URL 时要使用的特定路由。By defining a name for the route, you can specify a particular route to use when you use routing to construct a URL. 如果有多个路由与传递给该方法的值匹配,则指定特定路由非常重要 GetVirtualPathSpecifying a particular route is important when more than one route matches the values that you pass to the GetVirtualPath method. 如果未指定命名路由,ASP.NET 路由将使用与值匹配的集合中的第一个路由。If you do not specify a named route, ASP.NET routing will use the first route in the collection that matches the values. 有关详细信息,请参阅 如何:从路由构造 urlFor more information, see How to: Construct URLs from Routes.

名称只能在对象中使用一次 RouteCollectionA name can be used only one time in the RouteCollection object.

如果无需指定路由的名称,则可以通过调用方法来添加路由 AddIf you do not have to specify a name for the route, you can add the route by calling the Add method.

使用 GetReadLock 方法和 GetWriteLock 方法确保与集合进行交互,而不会与其他进程发生冲突。Use the GetReadLock method and the GetWriteLock method to make sure that you interact with the collection without conflicts from other processes.

适用于