[英]ASP.NET MVC3 Controller Method route variable parameters
有沒有一種方法可以將一組可變的參數路由到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 }
);
但這太脆弱了。
經過挖掘和一些離線幫助...
public ActionResult myaction(string allsegments)
{
var urlsegments = allsegments.split('/');
//...
}
routes.MapRoute("betterroute",
"myaction/{*allsegments}",
new { controller = "mycontroller", action = "myaction"}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.