3 Answers
3

The difference between the two is the following: Let's assume the Create method in question is on the HomeController.

Using the AcceptVerbs attribute does not affect routing. It's actually something used by the action invoker. What it allows you to do is have 2 action methods on a controller with the same name that each respond to a different HTTP Method.

So when a request for /home/create comes in, the route will match and hand off the request to the controller's invoker. The invoker then invokes the correct method by looking at the AcceptVerbs attribute.

Using the HttpMethodConstraint in routing will make it such that the route itself will not match the request. So when a POST request comes in for /home/create, neither action method will be called because that route will not match the request. It's possible that another route will match that request though.

Part of the reason for the overlap here is that Routing is a feature of ASP.NET 3.5 SP1 and isn't specific to MVC. MVC uses Routing, but Routing is also used by Dynamic Data and we plan to integrate routing with ASP.NET Web Forms.

@Phil: i understand the first part about how the controller's invoker picks the best method, based on AcceptVerbs. I don't understand the second part. Are you saying that if u use the HttpMethodConstraint, it wouldn't know which Create method to use?
–
Pure.KromeNov 13 '08 at 4:45

1

No, i'm saying that using a constraint means that the route itself doesn't match. If no route matches, then the request is not handed off to MVC in the first place.
–
HaackedNov 19 '08 at 8:47

@Haacked - This post is ages old, but I have a question regarding both these techniques. I'm using MVC3 and trying to build a JSON api for my app in a RESTful manner. This means I have a single route that needs to handle 3 verbs (POST, PUT, DELETE) - would it be smarter to use a constraint at the routing level and simply have 3 separate actions wired in the routes... or better to build an attribute in the MVC framework that captures multiple verbs and passes them as an argument to the action?
–
one.beat.consumerDec 22 '11 at 22:02

@Haacked - to correct myself and be more specific, GET, PUT, DELETE all share the same route signature /objName/{id} and I'm trying to have one Controller Action that handles accepts all three verbs -or- 3 actions (one for each verb) but the same name and same method signature (not possible with just attribs)
–
one.beat.consumerDec 22 '11 at 22:45

If you want the exact same method to handle them, use 3 attributes on it: [HttpPost][HttpGet][HttpDelete]public ActionResult MyMethod(){}
–
HaackedJan 6 '12 at 0:58

Yep - i understand all that, but that's not the question. What's the difference between the AcceptVerb attribute vs the HttpVerb constraint, defined in the route definition? Nothing ... just whatever way floats your boat?
–
Pure.KromeNov 12 '08 at 23:08