我有一个ASP.NET MVC 3应用程序。我正在尝试实现http://www.slideshare.net/calamitas/restful-best-practices处找到的路由标准。我正在使用幻灯片15和17作为参考。据我所知,这个幻灯片是关于RAILS的。但是,这种语法似乎更清晰,更自然。这就是我想要使用它的原因。
我已在控制器中成功实现了索引和显示操作。但是,我在创建和更新操作时遇到问题。此时,当我引用其中任何一个时,我收到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中正确设置了data属性。感谢您提供任何帮助。
答案 0 :(得分:2)
我认为你只需要下一个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
);
然后在你的控制器类中你需要这样的东西:
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);
}
}
请注意,您无需明确指定[HttpGet]
属性,如此link中所述。