Route.Constraints Proprietà

Definizione

Ottiene o imposta un dizionario di espressioni che specificano i valori validi per un parametro URL.

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

Valore della proprietà

Oggetto che contiene i nomi di parametro e le espressioni.

Esempio

Nell'esempio seguente viene illustrato come creare un Route oggetto e impostare le Constraintsproprietà , DataTokense 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

Nell'esempio seguente viene illustrato un Route oggetto la cui Constraints proprietà contiene un parametro denominato httpMethod con un'istanza della HttpMethodConstraint classe per il valore .

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

Commenti

La Constraints proprietà consente di limitare i valori accettabili per un parametro URL elaborato da una route. Si assegna un RouteValueDictionary oggetto alla Constraints proprietà . Ogni elemento nell'oggetto RouteValueDictionary contiene il nome di un parametro e uno dei seguenti:

  • Stringa che definisce un'espressione regolare. L'espressione regolare non fa distinzione tra maiuscole e minuscole.

  • Oggetto che implementa l'interfaccia IRouteConstraint e che include un Match metodo .

La classe HttpMethodConstraint implementa l'interfaccia IRouteConstraint. È possibile includere un'istanza della HttpMethodConstraint classe come vincolo per specificare quali verbi HTTP sono accettabili per la route.

Si applica a

Vedi anche