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?
Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.
When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table. The default route table contains a single route (named Default).
If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.
there are no limits in creating routes. You can create as many route as you want in your RouteConfig.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With