Asp.net-Mvc-2
ASP.NET MVC 2 參數數組
我需要有以下路由邏輯:
http://mydomain.com/myAction/{root}/{child1}/{child2}/…
我不知道路線的深度是多少,所以我希望動作的簽名看起來像這樣:
public ActionResult myAction(string[] hierarchy) { ... }不知道如何寫那條路線。幫助?
非常感謝。
添加以下映射時:
routes.MapRoute("hierarchy", "{action}/{*url}" new { controller = "Home", action = "Index" });您可以在操作方法中獲取字元串“url”:
public ActionResult myAction(string url) { ... }然後很容易獲得層次結構:
string[] hierarchy = url.Split('/');可以使用類似的方法從字元串值列表創建 url:
string firstPart = hierarchy.Count() > 0: hierarchy[0] : string.Empty; StringBuilder urlBuilder = new StringBuilder(firstPart); for (int index = 1; index < hierarchy.Count(); index++) { urlBuilder.Append("/"); urlBuilder.Append(hierarchy[index]); }然後可以在操作連結中使用 urlBuilder,例如:
<%= Html.ActionLink("Text", new { Controller="Home", Action="Index", Url=urlBuilder.ToString() }) %>