Route.DataTokens 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置传递到路由处理程序但未用于确定该路由是否与 URL 模式匹配的自定义值。
public:
property System::Web::Routing::RouteValueDictionary ^ DataTokens { System::Web::Routing::RouteValueDictionary ^ get(); void set(System::Web::Routing::RouteValueDictionary ^ value); };
public System.Web.Routing.RouteValueDictionary DataTokens { get; set; }
member this.DataTokens : System.Web.Routing.RouteValueDictionary with get, set
Public Property DataTokens As RouteValueDictionary
属性值
包含自定义值的对象。
示例
以下示例演示如何创建 Route 对象并设置 Constraints、 DataTokens和 Defaults 属性。
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
注解
使用 DataTokens 属性检索或分配与路由关联的值,这些值不用于确定路由是否与 URL 模式匹配。 这些值会传递到路由处理程序,以便用于处理请求。