ASP.NET MVC 2参数数组 [英] ASP.NET MVC 2 Parameter Array
问题描述
我需要有以下路由逻辑:
I need to have the following routing logic:
<一个href=\"http://mydomain.com/myAction/\">http://mydomain.com/myAction/{root}/{child1}/{child2}/...
http://mydomain.com/myAction/{root}/{child1}/{child2}/...
我不知道是什么路线的深度
所以我想动作的签名看起来就像是:
I don't know what is the depth of the route so I want the action's signature to look something like that:
public ActionResult myAction(string[] hierarchy)
{
...
}
不知道如何写这条路线。帮助?
Have no idea how to write that route. Help?
非常感谢。
推荐答案
当您添加以下映射:
routes.MapRoute("hierarchy", "{action}/{*url}"
new { controller = "Home", action = "Index" });
您可以获取字符串URL中的动作方法:
you can obtain the string 'url' in your action method:
public ActionResult myAction(string url)
{
...
}
然后轻松地获得层次:
The hierarchy is then easily obtained:
string[] hierarchy = url.Split('/');
从创建的字符串值列表的URL可以使用similair的方法来实现:
Creating an url from a list of string values can be done using a similair approach:
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然后可以在一个动作链接一起使用,例如:
urlBuilder can then be used in an action link, for example:
<%= Html.ActionLink("Text", new { Controller="Home", Action="Index", Url=urlBuilder.ToString() }) %>
这篇关于ASP.NET MVC 2参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!