2013-04-27 139 views
0

我正在嘗試創建自定義路由。這是我嘗試過的,但不起作用,我做錯了什麼?ASP.Net MVC - 創建自定義路由?

預期的調用:

MyWebsite/Friend/Respond/55/4 

routes.MapRoute(
      name : "Friend", 
      url : "Friend/Respond/{id}/{state}" 
); 

// This method is in a Controller Named FriendController 
[HttpPost] 
public ActionResult Respond(int id, int state) 
{ 
    // Do stuff 
} 

答:

routes.MapRoute(
      name : "ExtraParameter", 
      url : "{controller}/{action}/{id}/{state}", 
      defaults : new { } 
); 

回答

2

您可以發佈一個例子ActionLink的觸發的路線?

有你建立你的路線默認值:

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }); 

特別是在圖路線的第三個參數。您可能需要將您的ID和狀態參數設置爲UrlParameter.Optional

+0

是的,我有默認的路由,這裏是預期的鏈接:MyWebsite /朋友/響應/ 55/4。在沒有ActionLink的我從iOS客戶端 – aryaxt 2013-04-27 17:26:54

+0

調用這個服務你有沒有設置爲默認的朋友路線雖然?像這樣...'routes.MapRoute( 「Friend」, 「Friend/Respond/{id}/{state}」, new {controller =「Friend」,action =「Respond」,id = UrlParameter.Optional ,State = UrlParameter.Optional});' – wellers 2013-04-27 17:32:55

+1

不需要設置參數爲可選項,但設置「controller,and」action「was。解決了這個問題,謝謝,我希望根據它可以檢測到的url,但它不會't – aryaxt 2013-04-27 17:34:19

1

您可以設置ID和狀態UrlParameter.Optional。

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}/{state}", 
    new { controller = "yourcontrollername", action = "youraction", id = UrlParameter.Optional, state = UrlParameter.Optional 
    });