Skip to main content
.NET Framework Class Library
HttpMethodConstraint Class

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

Inheritance Hierarchy
System..::.Object
  System.Web.Routing..::.HttpMethodConstraint

Namespace: System.Web.Routing
Assembly: System.Web (in System.Web.dll)
Syntax
Public Class HttpMethodConstraint _
	Implements IRouteConstraint
public class HttpMethodConstraint : IRouteConstraint
public ref class HttpMethodConstraint : IRouteConstraint
type HttpMethodConstraint =  
    class
        interface IRouteConstraint
    end

The HttpMethodConstraint type exposes the following members.

Constructors
 NameDescription
Public methodHttpMethodConstraintInitializes a new instance of the HttpMethodConstraint class by using the HTTP verbs that are allowed for the route.
Top
Properties
 NameDescription
Public propertyAllowedMethodsGets the collection of allowed HTTP verbs for the route.
Top
Methods
 NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMatchDetermines whether the request was made with an HTTP verb that is one of the allowed verbs for the route.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Explicit Interface Implementations
 NameDescription
Explicit interface implemetationPrivate methodIRouteConstraint..::.MatchFor a description of this member, see Match.
Top
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.

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.


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


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);
}

Version Information

.NET Framework

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.