Infinite URL Parameters for ASP.NET MVC Route

tugberk picture tugberk · Sep 22, 2011 · Viewed 23.3k times · Source

I need an implementation where I can get infinite parameters on my ASP.NET Controller. It will be better if I give you an example :

Let's assume that I will have following urls :

example.com/tag/poo/bar/poobar
example.com/tag/poo/bar/poobar/poo2/poo4
example.com/tag/poo/bar/poobar/poo89

As you can see, it will get infinite number of tags after example.com/tag/ and slash will be a delimiter here.

On the controller I would like to do this :

foreach(string item in paramaters) { 

    //this is one of the url paramaters
    string poo = item;

}

Is there any known way to achieve this? How can I get reach the values from controller? With Dictionary<string, string> or List<string>?

NOTE :

The question is not well explained IMO but I tried my best to fit it. in. Feel free to tweak it

Answer

SLaks picture SLaks · Sep 22, 2011

Like this:

routes.MapRoute("Name", "tag/{*tags}", new { controller = ..., action = ... });

ActionResult MyAction(string tags) {
    foreach(string tag in tags.Split("/")) {
        ...
    }
}