ASP.NET MVC3 Controller方法路由变量参数

时间:2012-09-03 08:14:35

标签: c# asp.net-mvc-3 asp.net-mvc-routing

有没有办法将一组可变参数路由到MVC3控制器。

我正在努力获得匹配像

这样的网址的内容
/myaction/foo
or 
/myaction/foo/bar/widget/.../foo1/foo2/ - i.e. of unknown length.

此刻我正在伪装

public ActionResult myaction(string f1, string f2, string f3, string f4) 
{ 
}

和路线

 routes.MapRoute("brittleroute",
            "myaction/{f1}/{f2}/{f3}/{f4}",
            new { controller = "mycontroller", action = "myaction", f1 = UrlParameter.Optional, f2=UrlParameter.Optional, f3=UrlParameter.Optional, f4=UrlParameter.Optional }
            );

但这非常脆弱。

1 个答案:

答案 0 :(得分:1)

挖掘后和一些离线帮助...

public ActionResult myaction(string allsegments)
{
    var urlsegments = allsegments.split('/');
    //...
}


routes.MapRoute("betterroute",
        "myaction/{*allsegments}",
        new { controller = "mycontroller", action = "myaction"}
        );
相关问题