do I have to register the HttpVerb constraint in my route definition (when i'm registering routes) if i have decorated my action method with the [AcceptVerbs(..)] attribute already?
eg. i have this.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formCollection)
{ .. }
do i need to add this to the route that refers to this action, as a constraint?
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.
public ActionResult Create(int id) { .. }
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formCollection) { .. }
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.