2011-12-09 66 views
4

我有一個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中正確設置數據屬性。感謝您提供任何幫助。

+4

爲什麼你的兩個路由具有相同的網址是什麼? –

回答

2

您的網址缺少創建行動。將URL更改爲以下內容:

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

原因是您的路由從未達到爲訂單定義的第二個路由。所有以訂單開始的網址都將轉到訂單控制器和索引操作。

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

編輯:

你應該在這種情況下使用一般的路由:

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

如果URL是 '/訂單',那麼它會去 '指數',而是「/訂單/創建'將進入控制器中的'創建'操作。

+0

但在URL我把它說,像一個URL「/訂單/創建」不會是RESTful的。相反,主要的區別在於使用的Http Verb。我離開基地嗎?或者,ASP.NET MVC無法做到這一點嗎? –

+0

您必須將所有方法命名爲「Index」並且有不同的重載,或者您需要有一個名爲Index的方法,並在方法內部根據動詞做不同的邏輯。 –

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解釋。

+0

這是在正確的軌道上,但尚未完全。當您發佈信息時,您將發佈到列表中,而不是特定的ID,這是擊中Crud動作的唯一方法。所以你需要改變crud動作的名字索引。另外,當您嘗試在CRUD環境中工作時,您需要爲每個操作指定允許的http方法。 –

+0

@NickLarsen你是對的。我更新了我的答案,把'HttpPost'索引(我可以用輸入參數重載actionresult)'HttpDelete'。 – Galled