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.