Compartir vía


HttpMethodConstraint(String[]) Constructor

Definición

Inicializa una nueva instancia de la clase HttpMethodConstraint usando los verbos HTTP que se permiten para la ruta.

public:
 HttpMethodConstraint(... cli::array <System::String ^> ^ allowedMethods);
public HttpMethodConstraint (params string[] allowedMethods);
new System.Web.Routing.HttpMethodConstraint : string[] -> System.Web.Routing.HttpMethodConstraint
Public Sub New (ParamArray allowedMethods As String())

Parámetros

allowedMethods
String[]

Verbos HTTP que son válidos para la ruta.

Excepciones

El parámetro allowedMethods es null.

Ejemplos

En el ejemplo siguiente se muestra un Route objeto cuya Constraints propiedad contiene un elemento que tiene una clave denominada httpMethod y que tiene un valor que es una instancia de la HttpMethodConstraint clase .

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

Se aplica a

Consulte también