42

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

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
tugberk
  • 57,477
  • 67
  • 243
  • 335

5 Answers5

61

Like this:

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

ActionResult MyAction(string tags) {
    foreach(string tag in tags.Split("/")) {
        ...
    }
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    hmm, looks like so neat. gonna give it a try. – tugberk Sep 22 '11 at 13:40
  • what is the role of {*tags} there? Especially, *. – tugberk Sep 22 '11 at 13:43
  • 10
    That's a catch-all parameter. http://msdn.microsoft.com/en-us/library/cc668201.aspx#handling_a_variable_number_of_segments_in_a_url_pattern – SLaks Sep 22 '11 at 13:43
  • so, can we use all wildcard parameters on ASP.NET MVC framework like that or just *? – tugberk Sep 22 '11 at 13:45
  • Huh? `*` means catch-all. I don't know what you're asking. – SLaks Sep 22 '11 at 13:45
  • 3
    @tugberk: You can only use * and it always has to be the first character of a catch-all parameter. It is not a wildcard character in any way shape or form. It just means that this route parameter will catch everything from that point on in your URL. – Robert Koritnik Sep 22 '11 at 14:09
  • The direct link to the "Handling a variable number of segments" section is https://learn.microsoft.com/en-us/previous-versions/cc668201(v=vs.140)#handling-a-variable-number-of-segments-in-a-url-pattern – Eric Mutta Mar 13 '20 at 13:07
26

The catch all will give you the raw string. If you want a more elegant way to handle the data, you could always use a custom route handler.

public class AllPathRouteHandler : MvcRouteHandler
{
    private readonly string key;

    public AllPathRouteHandler(string key)
    {
        this.key = key;
    }

    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var allPaths = requestContext.RouteData.Values[key] as string;
        if (!string.IsNullOrEmpty(allPaths))
        {
            requestContext.RouteData.Values[key] = allPaths.Split('/');
        }
        return base.GetHttpHandler(requestContext);
    }
} 

Register the route handler.

routes.Add(new Route("tag/{*tags}",
        new RouteValueDictionary(
                new
                {
                    controller = "Tag",
                    action = "Index",
                }),
        new AllPathRouteHandler("tags")));

Get the tags as a array in the controller.

public ActionResult Index(string[] tags)
{
    // do something with tags
    return View();
}
TheCodeKing
  • 19,064
  • 3
  • 47
  • 70
12

That's called catch-all:

tag/{*tags}
talles
  • 14,356
  • 8
  • 45
  • 58
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
5

Just in case anyone is coming to this with MVC in .NET 4.0, you need to be careful where you define your routes. I was happily going to global.asax and adding routes as suggested in these answers (and in other tutorials) and getting nowhere. My routes all just defaulted to {controller}/{action}/{id}. Adding further segments to the URL gave me a 404 error. Then I discovered the RouteConfig.cs file in the App_Start folder. It turns out this file is called by global.asax in the Application_Start() method. So, in .NET 4.0, make sure you add your custom routes there. This article covers it beautifully.

DavidHyogo
  • 2,838
  • 4
  • 31
  • 48
1

in asp .net core you can use * in routing for example

[HTTPGet({*id})]

this code can multi parameter or when using send string with slash use them to get all parameters