简体   繁体   中英

ASP.NET MVC3 Controller Method route variable parameters

Is there a way to route a variable set of parameters to an MVC3 controller.

I'm trying to get something to match a url like

/myaction/foo
or 
/myaction/foo/bar/widget/.../foo1/foo2/ - i.e. of unknown length.

at the moment I'm faking it with

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

and a route

 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 }
            );

but that is horribly brittle.

After digging and some offline help...

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM