我有一個ASP.NET MVC 3應用程序。我正試圖實現在http://www.slideshare.net/calamitas/restful-best-practices找到的路由標準。我使用幻燈片15和17作爲參考。我明白這個幻燈片是關於RAILS的。但是,這個語法似乎更清晰和更自然。這就是爲什麼我想要使用它。ASP.NET MVC 3和寧靜的路由
我已經成功地實現了指數,並顯示在我的控制器動作。但是,我在使「創建」和「更新」操作起作用時遇到問題。在這個時候,當我可以引用這些,我收到了404。目前,我的控制器看起來是這樣的:
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);
}
}
當我登記我的路線,我用的是以下幾點:
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 = "" }
);
我試圖通過JQuery創建和更新。當我使用以下內容時:
// 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.");
}
});
我在做什麼錯?因爲它是一個404,我傾向於認爲這是一個不正確的路由。我認爲可能會有衝突,但我不知道如何證明或糾正這一點。同時,我不確定我是否在我的jQuery中正確設置數據屬性。感謝您提供任何幫助。
爲什麼你的兩個路由具有相同的網址是什麼? –