Infinite URL Parameters for ASP.NET MVC Route
Solution 1
Like this:
routes.MapRoute("Name", "tag/{*tags}", new { controller = ..., action = ... });
ActionResult MyAction(string tags) {
foreach(string tag in tags.Split("/")) {
...
}
}
Solution 2
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();
}
Solution 3
That's called catch-all:
tag/{*tags}
Solution 4
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.
Solution 5
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
tugberk
Senior Software Engineer and Tech Lead, with a growth mindset belief and 10+ years of practical software engineering experience including technical leadership and distributed systems. I have a passion to create impactful software products, and I care about usability, reliability, observability and scalability of the software systems that I work on, as much as caring about day-to-day effectiveness, productivity and happiness of the team that I work with. I occasionally speak at international conferences (tugberkugurlu.com/speaking), and write technical posts on my blog (tugberkugurlu.com). I currently work at Facebook as a Software Engineer. I used to work at Deliveroo as a Staff Software Engineer in the Consumer division, working on distributed backend systems which have high throughput, low latency and high availability needs. Before that, I used to work at Redgate as a Technical Lead for 4 years, where I led and line-managed a team of 5 Software Engineers. I was responsible for all aspects of the products delivered by the team from technical architecture to product direction. I was also a Microsoft MVP for 7 years between 2012-2019 on Microsoft development technologies.
Updated on May 11, 2021Comments
-
tugberk almost 3 years
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>
orList<string>
?NOTE :
The question is not well explained IMO but I tried my best to fit it. in. Feel free to tweak it
-
tugberk over 12 yearshmm, looks like so neat. gonna give it a try.
-
tugberk over 12 yearswhat is the role of {*tags} there? Especially, *.
-
SLaks over 12 yearsThat's a catch-all parameter. msdn.microsoft.com/en-us/library/…
-
tugberk over 12 yearsso, can we use all wildcard parameters on ASP.NET MVC framework like that or just *?
-
SLaks over 12 yearsHuh?
*
means catch-all. I don't know what you're asking. -
Robert Koritnik over 12 years@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.
-
tugberk over 12 years@RobertKoritnik that's what I needed. Thanks!
-
Eric Mutta about 4 yearsThe direct link to the "Handling a variable number of segments" section is docs.microsoft.com/en-us/previous-versions/…