Route.Constraints Propriedade

Definição

Obtém ou define um dicionário de expressões que especificam os valores válidos para um parâmetro de URL.Gets or sets a dictionary of expressions that specify valid values for a URL parameter.

public:
 property System::Web::Routing::RouteValueDictionary ^ Constraints { System::Web::Routing::RouteValueDictionary ^ get(); void set(System::Web::Routing::RouteValueDictionary ^ value); };
public System.Web.Routing.RouteValueDictionary Constraints { get; set; }
member this.Constraints : System.Web.Routing.RouteValueDictionary with get, set
Public Property Constraints As RouteValueDictionary

Valor da propriedade

Um objeto que contém os nomes e as expressões de parâmetro.An object that contains the parameter names and expressions.

Exemplos

O exemplo a seguir mostra como criar um objeto Route e definir as propriedades Constraints, DataTokense Defaults.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

O exemplo a seguir mostra um objeto Route cuja propriedade Constraints contém um parâmetro chamado httpMethod com uma instância da classe HttpMethodConstraint para o valor.The following example shows a Route object whose Constraints property contains a parameter named httpMethod with an instance of the HttpMethodConstraint class for the value.

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

public static void RegisterRoutes(RouteCollection routes)
{
    string[] allowedMethods = { "GET", "POST" };
    HttpMethodConstraint methodConstraints = new HttpMethodConstraint(allowedMethods);

    Route reportRoute = new Route("{locale}/{year}", new ReportRouteHandler());
    reportRoute.Constraints = new RouteValueDictionary { { "httpMethod", methodConstraints } };

    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
    Dim allowedMethods() As String = {"GET", "POST"}
    Dim methodConstraints As HttpMethodConstraint  
    
    methodConstraints = New HttpMethodConstraint(allowedMethods)
    
    Dim constraintValues = New With {.httpMethod = methodConstraints}
    
    urlPattern = "{locale}/{year}"
    
    reportRoute = New Route(urlPattern, New ReportRouteHandler)
    reportRoute.Constraints = New RouteValueDictionary(constraintValues)
    
    routes.Add(reportRoute)
End Sub

Comentários

A propriedade Constraints permite limitar valores aceitáveis para um parâmetro de URL que é processado por uma rota.The Constraints property enables you to limit acceptable values for a URL parameter that is processed by a route. Você atribui um objeto RouteValueDictionary à propriedade Constraints.You assign a RouteValueDictionary object to the Constraints property. Cada elemento no objeto de RouteValueDictionary contém o nome de um parâmetro e um dos seguintes:Each element in the RouteValueDictionary object contains the name of a parameter and one of the following:

  • Uma cadeia de caracteres que define uma expressão regular.A string that defines a regular expression. A expressão regular não diferencia maiúsculas de minúsculas.The regular expression is case-insensitive.

  • Um objeto que implementa a interface IRouteConstraint e que inclui um método Match.An object that implements the IRouteConstraint interface and that includes a Match method.

A classe HttpMethodConstraint implementa a interface IRouteConstraint.The HttpMethodConstraint class implements the IRouteConstraint interface. Você pode incluir uma instância da classe HttpMethodConstraint como uma restrição para especificar quais verbos HTTP são aceitáveis para a rota.You can include an instance of the HttpMethodConstraint class as a constraint to specify which HTTP verbs are acceptable for the route.

Aplica-se a

Veja também