我有一个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中正确设置数据属性。感谢您提供任何帮助。
为什么你的两个路由具有相同的网址是什么? –