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" }) });
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.
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" });
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.
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.
[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) { }
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