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
116k39 gold badges204 silver badges294 bronze badges
asked Sep 22, 2011 at 13:36

5 Answers 5

61

Like this:

routes.MapRoute("Name", "tag/{*tags}", new { controller = ..., action = ... });
ActionResult MyAction(string tags) {
 foreach(string tag in tags.Split("/")) {
 ...
 }
}
answered Sep 22, 2011 at 13:39
Sign up to request clarification or add additional context in comments.

7 Comments

hmm, looks like so neat. gonna give it a try.
what is the role of {*tags} there? Especially, *.
That's a catch-all parameter. msdn.microsoft.com/en-us/library/…
so, can we use all wildcard parameters on ASP.NET MVC framework like that or just *?
@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.
|
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();
}
answered Sep 22, 2011 at 14:47

Comments

12

That's called catch-all:

tag/{*tags}
talles
15.4k10 gold badges48 silver badges63 bronze badges
answered Sep 22, 2011 at 13:43

Comments

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.

answered Feb 12, 2013 at 15:13

Comments

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

answered May 11, 2021 at 4:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.