I want to have some controller with the single method that would allow me to navigate through some hierarchy (file system etc.).
In other words I want to have possibility to access this method with flexible routes and get part of routes as parameter. For example in case of this hierarchy
Root
Sub-folder-A
Sub-folder-B
Sub-folder-C
I want to have access folders with the next routes
mymvcapplication/explorer/root
mymvcapplication/explorer/root/sub-folder-a
mymvcapplication/explorer/root/sub-folder-b/sub-folder-c
What and where should I configure to implement it properly?
To support variable number of url parameter values in the request url, you can mark your method parameter with
*
prefix in the route definition.With MVC Attribute routing,
The last parameter prefixed with
*
is like a catch-all parameter which will store anything in the url afterexplorer/root
So when you request
yoursite.com/explorer/root/a/b/c/d
, the default model binder will map the value"a/b/c/d"
to thelevels
parameter. You can call theSplit
method on that string to get an array of url segments.To enable attribute routing, go to
RouteConfig.cs
and call theMapMvcAttributeRoutes()
method in theRegisterRoutes
.