Bewerken

Delen via


HttpMethodConstraint Class

Definition

Enables you to define which HTTP verbs are allowed when ASP.NET routing determines whether a URL matches a route.

public ref class HttpMethodConstraint : System::Web::Routing::IRouteConstraint
public class HttpMethodConstraint : System.Web.Routing.IRouteConstraint
type HttpMethodConstraint = class
    interface IRouteConstraint
Public Class HttpMethodConstraint
Implements IRouteConstraint
Inheritance
HttpMethodConstraint
Implements

Examples

The following example shows a Route object whose Constraints property contains an item that has a key named httpMethod and that has a value that is an instance of the HttpMethodConstraint class.

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

Remarks

The HttpMethodConstraint class enables you to limit route matching to certain HTTP verbs. For example, you can specify that a route is considered a match for a URL only when the HTTP verb for the request is POST.

To set constraints for route matching, you set the Constraints property of the Route class to an instance of the RouteValueDictionary class. To set an HTTP verb constraint, you set the value of one dictionary element to an HttpMethodConstraint object and the key to any name.

In the HttpMethodConstraint method, you include all the HTTP verbs that are allowed for the route when ASP.NET routing determines whether the route matches a URL.

Constructors

HttpMethodConstraint(String[])

Initializes a new instance of the HttpMethodConstraint class by using the HTTP verbs that are allowed for the route.

Properties

AllowedMethods

Gets the collection of allowed HTTP verbs for the route.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
Match(HttpContextBase, Route, String, RouteValueDictionary, RouteDirection)

Determines whether the request was made with an HTTP verb that is one of the allowed verbs for the route.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IRouteConstraint.Match(HttpContextBase, Route, String, RouteValueDictionary, RouteDirection)

For a description of this member, see Match(HttpContextBase, Route, String, RouteValueDictionary, RouteDirection).

Applies to

See also