Infinite URL Parameters for ASP.NET MVC Route
Asked Answered
N

5

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

Navigable answered 22/9, 2011 at 13:36 Comment(0)
H
61

Like this:

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

ActionResult MyAction(string tags) {
    foreach(string tag in tags.Split("/")) {
        ...
    }
}
Hutt answered 22/9, 2011 at 13:39 Comment(7)
hmm, looks like so neat. gonna give it a try.Navigable
what is the role of {*tags} there? Especially, *.Navigable
That's a catch-all parameter. msdn.microsoft.com/en-us/library/…Hutt
so, can we use all wildcard parameters on ASP.NET MVC framework like that or just *?Navigable
Huh? * means catch-all. I don't know what you're asking.Hutt
@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.Ferrotype
The direct link to the "Handling a variable number of segments" section is learn.microsoft.com/en-us/previous-versions/…Reprehensible
S
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();
}
Seductive answered 22/9, 2011 at 14:47 Comment(0)
G
12

That's called catch-all:

tag/{*tags}
Gunslinger answered 22/9, 2011 at 13:43 Comment(0)
E
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.

Exosmosis answered 12/2, 2013 at 15:13 Comment(0)
I
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

Insistent answered 11/5, 2021 at 4:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.