Export (0) Print
Expand All

Route.Defaults Property

Gets or sets the values to use if the URL does not contain all the parameters.

Namespace:  System.Web.Routing
Assembly:  System.Web (in System.Web.dll)

public RouteValueDictionary Defaults { get; set; }

Property Value

Type: System.Web.Routing.RouteValueDictionary
An object that contains the parameter names and default values.

The Defaults property enables you to set the value for a URL parameter if the URL does not contain a segment for that parameter. You assign a RouteValueDictionary object to the Defaults property. Each element in the RouteValueDictionary object contains the name of a parameter and the value to use if the parameter is missing.

You can include a default value for a parameter that is not defined in the Url property as a segment. When ASP.NET routing handles a request, this default value is always passed to the route handler. When you construct a URL and you include a value for a default parameter that is not defined as a segment, the route will only be considered a match if the value that you provided matches the default value for the route.

The following example shows how to create a Route object and set the Constraints, DataTokens, and Defaults properties.


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);
}


The following example shows a Route object whose Defaults property contains a parameter that is not part of the pattern in the Url property.


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);
}


.NET Framework

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft