Route.Defaults 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 URL 未包含所有參數時所要使用的值。
public:
property System::Web::Routing::RouteValueDictionary ^ Defaults { System::Web::Routing::RouteValueDictionary ^ get(); void set(System::Web::Routing::RouteValueDictionary ^ value); };
public System.Web.Routing.RouteValueDictionary Defaults { get; set; }
member this.Defaults : System.Web.Routing.RouteValueDictionary with get, set
Public Property Defaults 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
下列範例顯示 Route 物件,其 Defaults 屬性包含不屬於 屬性中 Url 模式的參數。
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 { { "months", "all" } };
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 {.months = "all"})
routes.Add(reportRoute)
End Sub
備註
如果 URL 不包含該參數的區段,此屬性 Defaults 可讓您設定 URL 參數的值。 您會將物件指派 RouteValueDictionary 給 Defaults 屬性。 物件中的每個 RouteValueDictionary 元素都包含參數的名稱,以及遺漏參數時要使用的值。
您可以包含屬性中未定義為區段的參數 Url 預設值。 當 ASP.NET 路由處理要求時,此預設值一律會傳遞至路由處理程式。 當您建構 URL 並包含未定義為區段的預設參數值時,只有在您提供的值符合路由的預設值時,才會將路由視為相符專案。