views:

6650

answers:

3

Hi Folks,

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?

+3  A: 

Nope -- Create will only respond to POST requests.

You can have other implementations of Create with different AcceptVerb attributes, or one with no attribute that will catch all other requests.

If that was your only Create method, any GET (or other non-POST) request would result in a 404.

I assume under the hood this is all being done by the routing engine anyways. [edit: nope, see Haacked's post]

eyston
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.Krome
+13  A: 

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 request 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.

Haacked
@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.Krome
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.
Haacked
Yay :) more info for me, nice answer!
Ropstah
A: 

First decorate like this:

[ActionName("ItemEdit"), AcceptVerbs(HttpVerbs.Post)]
public virtual object ItemSave(Menu sampleInput)

then you need to add route like this:

 AddRoute(
                "SampleEdit",
                "Admin/{sampleID}/Edit",
                new { controller = "Sample", action = "ItemEdit", validateAntiForgeryToken = true },
                new { areaID = new IsGuid() },
                new { Namespaces = controllerNamespaces }
           );
MrByte