RouteCollection.Add(String, RouteBase) 方法

定義

將路由加入 RouteCollection 物件的結尾,並將指定的名稱指派給路由。

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

識別路由的值。 這個值可以是 null 或空字串。

item
RouteBase

要加入集合結尾的路由。

例外狀況

itemnull

name 已經在集合中使用。

範例

下列範例示範如何將路由新增至 RouteCollection 物件,並將名稱指派給路由。

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 可讓您在將路由新增至路由集合時定義路由的名稱。 藉由定義路由的名稱,您可以在使用路由來建構 URL 時指定要使用的特定路由。 當多個路由符合您傳遞至 GetVirtualPath 方法的值時,指定特定路由很重要。 如果您未指定具名路由,ASP.NET 路由將會使用集合中符合值的第一個路由。 如需詳細資訊,請參閱 如何:從路由建構 URL

名稱只能在 物件中使用 RouteCollection 一次。

如果您不需要指定路由的名稱,您可以藉由呼叫 Add 方法來新增路由。

GetReadLock使用 方法和 方法 GetWriteLock ,確定您與集合互動,而不會與其他進程發生衝突。

適用於

另請參閱