RouteValueDictionary 构造函数

定义

初始化 RouteValueDictionary 类的新实例。

重载

RouteValueDictionary()

初始化为空的 RouteValueDictionary 类的新实例。

RouteValueDictionary(IDictionary<String,Object>)

初始化 RouteValueDictionary 类的新实例,并从指定集合添加元素。

RouteValueDictionary(Object)

初始化 RouteValueDictionary 类的新实例,并添加基于指定对象的属性的值。

RouteValueDictionary()

初始化为空的 RouteValueDictionary 类的新实例。

public:
 RouteValueDictionary();
public RouteValueDictionary ();
Public Sub New ()

注解

可以通过调用 Add 方法向 RouteValueDictionary 对象添加元素。

另请参阅

适用于

RouteValueDictionary(IDictionary<String,Object>)

初始化 RouteValueDictionary 类的新实例,并从指定集合添加元素。

public:
 RouteValueDictionary(System::Collections::Generic::IDictionary<System::String ^, System::Object ^> ^ dictionary);
public RouteValueDictionary (System.Collections.Generic.IDictionary<string,object> dictionary);
new System.Web.Routing.RouteValueDictionary : System.Collections.Generic.IDictionary<string, obj> -> System.Web.Routing.RouteValueDictionary
Public Sub New (dictionary As IDictionary(Of String, Object))

参数

dictionary
IDictionary<String,Object>

一个集合,其元素被复制到新集合中。

例外

dictionarynull

dictionary 包含一个或多个重复键。

注解

对象中的每个键都必须是唯一的 RouteValueDictionary 。 类 RouteValueDictionary 使用不区分大小写的序号比较。 有关详细信息,请参阅 OrdinalIgnoreCase。 键不能为 null

另请参阅

适用于

RouteValueDictionary(Object)

初始化 RouteValueDictionary 类的新实例,并添加基于指定对象的属性的值。

public:
 RouteValueDictionary(System::Object ^ values);
public RouteValueDictionary (object values);
new System.Web.Routing.RouteValueDictionary : obj -> System.Web.Routing.RouteValueDictionary
Public Sub New (values As Object)

参数

values
Object

一个对象,其中包含将作为元素添加到新集合的属性。

示例

以下示例演示如何创建 Route 对象以及如何设置 ConstraintsDataTokensDefaults 属性。

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    Route reportRoute = new Route("{locale}/{year}", new ReportRouteHandler());
    reportRoute.Defaults = new RouteValueDictionary { { "locale", "en-US" }, { "year", DateTime.Now.Year.ToString() } };
    reportRoute.Constraints = new RouteValueDictionary { { "locale", "[a-z]{2}-[a-z]{2}" }, { "year", @"\d{4}" } };
    reportRoute.DataTokens = new RouteValueDictionary { { "format", "short" } };
    routes.Add(reportRoute);
}
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 reportRoute As Route
    
    urlPattern = "{locale}/{year}"
    
    reportRoute = New Route(urlPattern, New ReportRouteHandler)
    reportRoute.Defaults = New RouteValueDictionary(New With {.locale = "en-US", .year = DateTime.Now.Year.ToString()})
    reportRoute.Constraints = New RouteValueDictionary(New With {.locale = "[a-z]{2}-[a-z]{2}", .year = "\d{4}"})
    reportRoute.DataTokens = New RouteValueDictionary(New With {.format = "short"})

    routes.Add(reportRoute)
End Sub

注解

根据字典的相等比较器,对象中的每个键都必须是唯一 RouteValueDictionary 的。 类 RouteValueDictionary 使用不区分大小写的序号比较。 有关详细信息,请参阅 OrdinalIgnoreCase。 键不能为 null

另请参阅

适用于