Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to route GET and DELETE requests for the same url to different controller methods

Tags:

Here are my routes in Global.asax

    routes.MapRoute("PizzaGet", "pizza/{pizzaKey}", new { controller = "Pizza", action = "GetPizzaById" });     routes.MapRoute("DeletePizza", "pizza/{pizzaKey}", new { controller = "Pizza", action = "DeletePizza" });     

Here are the my controller methods

[AcceptVerbs(HttpVerbs.Get)] public ActionResult GetPizzaById(long pizzaKey)  [AcceptVerbs(HttpVerbs.Delete)] public ActionResult DeletePizza(long pizzaKey) 

When I do a GET it returns the object, but when I do a DELETE I get a 404. It seems like this should work, but it doesn't.

If I switch the two routes around then I can do the DELETE, but get a 404 on the GET.

Now this is truly beautiful. Thanks

routes.MapRoute("Pizza-GET","pizza/{pizzaKey}",               new { controller = "Pizza", action = "GetPizza"},               new { httpMethod = new HttpMethodConstraint(new string[]{"GET"})});              routes.MapRoute("Pizza-UPDATE", "pizza/{pizzaKey}",               new { controller = "Pizza", action = "UpdatePizza" },               new { httpMethod = new HttpMethodConstraint(new string[] { "PUT" }) });              routes.MapRoute("Pizza-DELETE", "pizza/{pizzaKey}",               new { controller = "Pizza", action = "DeletePizza" },               new { httpMethod = new HttpMethodConstraint(new string[] { "DELETE" }) });              routes.MapRoute("Pizza-ADD", "pizza/",               new { controller = "Pizza", action = "AddPizza" },               new { httpMethod = new HttpMethodConstraint(new string[] { "POST" }) }); 
like image 422
Arron S Avatar asked Jan 27 '10 01:01

Arron S


People also ask

What is URL RouteUrl?

RouteUrl(String, Object) Generates a fully qualified URL for the specified route values by using a route name. RouteUrl(String, RouteValueDictionary) Generates a fully qualified URL for the specified route values by using a route name.

Can we map multiple URLs to the same action with example?

Yes, We can use multiple URLs to the same action with the use of a routing table. foreach(string url in urls)routes. MapRoute("RouteName-" + url, url, new { controller = "Page", action = "Index" });

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

What is routes MapRoute in MVC?

Routing in ASP.NET MVC By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional). Now let us create one MVC Application and open RouteConfig.


1 Answers

[ActionName("Pizza"), HttpPost] public ActionResult Pizza_Post(int theParameter) { }  [ActionName("Pizza"), HttpGet] public ActionResult Pizza_Get(int theParameter) { }  [ActionName("Pizza"), HttpHut] public ActionResult Pizza_Hut(int theParameter) { } 
like image 69
Levi Avatar answered Sep 29 '22 15:09

Levi