2011-06-06 60 views
0

我有4條路由定義了5個不同的URL。與RouteDebugger測試了很多,但無法解決。路由在我的asp.net應用中不起作用

的問題是,前2個鏈接始終使用{控制器}/{行動}/{ID}這條路線是目錄root1而不能重定向到正確的頁面。

鏈接

@Html.ActionLink("Go Index by name", "Page", "Home", new { name="contact"}, null) 

@Html.ActionLink("Go Index by id", "Index", "Admin", new { id=2}, null) 


@Html.ActionLink("Go Index by id and name", "Page", "Home", new { name = "contact", id = 2 }, null) 


@Html.ActionLink("Root Admin", "Index", "Admin") 


@Html.ActionLink("Root", "Index", "Home") 

這裏是Map.Route

routes.MapRoute("root1", 
     "{controller}/{action}/{id}", 
     new { controller = "Admin", action = "Index" }); 

    routes.MapRoute("root2", 
     "{controller}/{action}/{name}", 
     new { controller = "Home", action = "Page" }); 

    routes.MapRoute("root3", 
     "{controller}/{action}/{name}/{id}", 
     new { controller = "Home", action = "Page" }); 

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

回答

1

這些是我建立的路線,它似乎正確地擊中每一個。

請注意,root3已被移動到頂端,因爲root2也會匹配。此外,作爲國王朱利安驗證了root1id建議

路線:

@Html.ActionLink("Root Admin", "Index", "Admin") 

不應該因爲有和沒有默認idname分別在路線定義匹配root1也不root2

routes.MapRoute("root3", 
    "{controller}/{action}/{name}/{id}", 
     new { controller = "Home", action = "Page" }); 

routes.MapRoute("root1", 
     "{controller}/{action}/{id}", 
     new { controller = "Admin", action = "Index" }, 
     new { id = @"\d+" }); 

routes.MapRoute("root2", 
     "{controller}/{action}/{name}", 
     new { controller = "Home", action = "Page" }); 

routes.MapRoute("root4", 
     "{controller}/{action}/{name}", 
     new { controller = "Home", action = "Index", name = UrlParameter.Optional  
}); 
+0

感謝您的回覆。如何處理@ Html.ActionLink(「Root Admin」,「Index」,「Admin」)路徑問題。我不想分配id = 0來使其工作。請任何解決方案? – Pirzada 2011-06-07 15:00:37

+0

@pirzada,我不明白是什麼問題 - 該鏈接與'root4'相匹配,並顯示爲「http:// foo.com/Admin」,我相信這是正確的事情 – Beno 2011-06-07 23:06:13

1

添加約束你的路由。例如:

routes.MapRoute(
    "root1", 
    "{controller}/{action}/{id}", 
    new { controller = "Admin", action = "Index" }, 
    new {id = @"\d+" } 
); 

確保root1只在id爲整數時才匹配。否則,root2會抓住它。

+0

謝謝我做了你說的,但我的@ Html.ActionLink(「根管理員」,「索引」,「管理員」)catch root1。這應該打到root4。請幫忙 – Pirzada 2011-06-06 21:26:56