HttpMethodConstraint Class

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

Namespace:  System.Web.Routing
Assembly:  System.Web.Routing (in System.Web.Routing.dll)

[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class HttpMethodConstraint : IRouteConstraint

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.

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

System.Object
  System.Web.Routing.HttpMethodConstraint

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft