2016-10-26 40 views
1

可以說,我已經兩個Web地址ASP.NET MVC可選頁參數

http://www.example.com/page/one 
http://www.example.com/page/one/subOne 

我如何讓他們由同一個控制器來處理。目前正在處理頂端地址,但第二個正在由不顯示頁面的回發函數處理。

所以對於我的路由配置,我有

routes.MapRoute("PageRule", "page/{id}", new { Controller = "document", action = "Index", id = UrlParameter.Optional }); 
routes.MapRoute("Page2Rule","page/{id}/{misc}", new { Controller = "document", action = "Index", id = UrlParameter.Optional }); 

,並在控制器我有

// GET: Document 
public ActionResult Index(string id, string misc) 

// Post 
[HttpPost] 
public ActionResult postcomment(string gCaptcha,string txtName, string txtEmail, string txtMessage) 
+0

請嘗試閱讀本文:http://stackoverflow.com/questions/5061249/asp-mvc-使用兩個可選參數進行路由 – David

回答

2

我懷疑這可能是你需要的唯一途徑:

routes.MapRoute("PageRule", 
       "page/{id}/{misc}", 
       new 
       { 
        Controller = "document", 
        Action = "Index", 
        misc = UrlParameter.Optional 
       }); 

請注意,id沒有可選項。關鍵是idmisc不能同時選擇 - 只有路線中的最後一個參數可以是可選的

+0

謝謝,我的代碼有兩個錯誤,我注意到當我只需要你指出的一個時,我有兩條規則。其次,我有id作爲可選,它應該misc是可選的。 – MiscellaneousUser