ASP.NET MVC 3 and quiet routing

I have an ASP.NET MVC 3 application. I am trying to implement the routing standard found at http://www.slideshare.net/calamitas/restful-best-practices . I use slides 15 and 17 for reference. I understand that this slide deck belongs to RAILS. However, this syntax seems so cleaner and more natural. That is why I want to use it.

I have successfully performed Index and Show actions in my controller. However, I am having trouble working on creating and updating. At this time, when I reference any of them, I get 404. Currently, my controller looks like this:

public class OrdersController : Controller { // GET: /Orders/ public ActionResult Index() { var results = new[] { new {id=1, price=1.23, quantity=2} }; return Json(results, JsonRequestBehavior.AllowGet); } // // GET: /Orders/{orderID} public ActionResult Show(int id) { string result = "order:" + id; return Json(result, JsonRequestBehavior.AllowGet); } // // POST: /Orders/{order} [HttpPost] public ActionResult Create(object order) { var message = "The order was successfully created!"; return Json(message); } // // PUT: /Orders/{orderID} [HttpPut] public ActionResult Update(object orderID) { var message = "The order was successfully updated!"; return Json(message); } } 

When I register my routes, I use the following:

 context.MapRoute( "OrderList", "Orders", new { action = "Index", controller="Orders" } ); context.MapRoute( "Order", "Orders/{id}", new { action = "Show", controller = "Orders", id="" } ); context.MapRoute( "InsertOrder", "Orders", new { action = "Create", controller = "Orders" } ); context.MapRoute( "UpdateOrder", "Orders/{orderID}", new { action = "Update", controller = "Orders", orderID = "" } ); 

I am trying to create CREATE and UPDATE through jQuery. When I use the following:

 // Update var order = getOrder(); $.ajax({ url: "/orders", type: "put", data: JSON.stringify(order), contentType: "application/json", success: function (result) { alert(result); }, error: function () { alert("There was a problem."); } }); // Create var order = getOrder(); $.ajax({ url: "/orders", type: "post", data: JSON.stringify(order), contentType: "application/json", success: function (result) { alert(result); }, error: function () { alert("There was a problem."); } }); 

What am I doing wrong? Since its 404, I am inclined to believe that this is the wrong routing. I think there may be a conflict, but I do not know how to prove or fix it. At the same time, I'm not sure if I set the data property in my jQuery correctly. Thanks for any help that can be provided.

+4
source share
2 answers

Your url is missing an action to create. Change the url to the following:

 $.ajax({ url: "/orders/create", .... 

The reason is because your routing never reaches the second one defined for orders. The entire url starting with Orders will go to the Order controller and the order.

 context.MapRoute( "OrderList", "Orders", new { action = "Index", controller="Orders" } ); 

EDIT:

In this case, you should use general routing:

 context.MapRoute( "Order", "Orders/{action}/{id}", new { action = "Index", controller = "Orders", id="" } ); 

If the URL is “/ Orders”, it will go to “Index”, but “/ Orders / Create” will go to the “Create” action in the controller.

+2
source

I think you only need the following MapRoutes:

 routes.MapRoute( "OrdersIndex", // Route name "{controller}", // URL with parameters new { controller = "Orders", action = "Index" } // Parameter defaults ); routes.MapRoute( "OrdersCrud", // Route name "{controller}/{id}", // URL with parameters new { controller = "Orders", action = "Crud" } // Parameter defaults ); 

Then in your controller class you need something like this:

 public class OrdersController : Controller { // GET: /Orders/ [HttpGet] //GET is by default public ActionResult Index() { var results = new[] { new {id=1, price=1.23, quantity=2} }; return Json(results, JsonRequestBehavior.AllowGet); } // // POST: /Orders/ [HttpPost] public ActionResult Index(object order) { //Create var message = "The order was successfully created!"; return Json(message); } // // GET: /Orders/{orderID} [HttpGet]//GET is by default public ActionResult Crud(int id) { //Show string result = "order:" + id; return Json(result, JsonRequestBehavior.AllowGet); } // // PUT: /Orders/{orderID} [HttpPut] public ActionResult Crud(object orderWithIDProperty) { //Update var message = "The order was successfully updated!"; return Json(message); } // // DELETE: /Orders/{orderID} [HttpDelete] public ActionResult Crud(int id) { //Delete var message = "The order was successfully deleted!"; return Json(message); } } 

Please note that you do not need to explicitly specify the [HttpGet] attribute as described in this link .

+2
source

Source: https://habr.com/ru/post/1385560/


All Articles