Route.Defaults Route.Defaults Route.Defaults Route.Defaults Property

Definición

Obtiene o establece los valores que se usarán si la dirección URL no contiene todos los parámetros.Gets or sets the values to use if the URL does not contain all the parameters.

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

Valor de propiedad

Objeto que contiene los nombres y los valores predeterminados de los parámetros.An object that contains the parameter names and default values.

Ejemplos

En el ejemplo siguiente se muestra cómo crear Route un objeto y establecer Constraintslas DataTokenspropiedades, Defaults y.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);
}
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

En el ejemplo siguiente se Route muestra un Defaults objeto cuya propiedad contiene un parámetro que no forma parte del patrón en Url la propiedad.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);
}
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

Comentarios

La Defaults propiedad permite establecer el valor de un parámetro de dirección URL si la dirección URL no contiene un segmento para ese parámetro.The Defaults property enables you to set the value for a URL parameter if the URL does not contain a segment for that parameter. Un RouteValueDictionary objeto se asigna a la Defaults propiedad.You assign a RouteValueDictionary object to the Defaults property. Cada elemento RouteValueDictionary del objeto contiene el nombre de un parámetro y el valor que se va a usar si falta el parámetro.Each element in the RouteValueDictionary object contains the name of a parameter and the value to use if the parameter is missing.

Puede incluir un valor predeterminado para un parámetro que no está definido en la Url propiedad como un segmento.You can include a default value for a parameter that is not defined in the Url property as a segment. Cuando el enrutamiento de ASP.NET controla una solicitud, este valor predeterminado siempre se pasa al controlador de ruta.When ASP.NET routing handles a request, this default value is always passed to the route handler. Cuando se crea una dirección URL y se incluye un valor para un parámetro predeterminado que no está definido como un segmento, la ruta solo se considerará una coincidencia si el valor proporcionado coincide con el valor predeterminado de la ruta.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.

Se aplica a

Consulte también: