Infinite URL Parameters for ASP.NET MVC Route Infinite URL Parameters for ASP.NET MVC Route asp.net asp.net

Infinite URL Parameters for ASP.NET MVC Route


Like this:

routes.MapRoute("Name", "tag/{*tags}", new { controller = ..., action = ... });ActionResult MyAction(string tags) {    foreach(string tag in tags.Split("/")) {        ...    }}


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();}