Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC AcceptVerbs and registering routes

Tags:

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?

like image 976
Pure.Krome Avatar asked Nov 12 '08 07:11

Pure.Krome


People also ask

Where are routes registered in ASP.NET MVC application?

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.

Which method is used for adding routes to an MVC application?

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

What is the difference between Web API routing and MVC routing?

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.

How many routes can be defined in the MVC 3 application?

there are no limits in creating routes. You can create as many route as you want in your RouteConfig.


2 Answers

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.

like image 74
Haacked Avatar answered Sep 28 '22 05:09

Haacked


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]

like image 25
anonymous Avatar answered Sep 28 '22 04:09

anonymous