Andiih
Andiih

Reputation: 12413

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.

Upvotes: 0

Views: 769

Answers (1)

Andiih
Andiih

Reputation: 12413

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

Upvotes: 1

Related Questions