Reputation: 23078
I have a data model for a directory type structure that looks something like this:
Folder:
String Name
Folder Parent
List<File> Files
File
Folder Parent
Content
Files and folders at the root level would have a null parent. For navigating folders I would like to be able to have urls that look something like:
http://mysite.com/directoryA/directoryB/directoryC/
I realize this has a bit of redundancy, but I feel it has better readability, and makes a good deal of sense in this case. Using default routes, that same folder would look like:
http://mysite.com/Folder/directorycID
which does not read nearly as nice. Are there any existing patterns that can do this type of routing? The problem with using names instead of ID's is that I would have to trace the entire structure from the root, since names are only guaranteed unique within a parent directory. Is there a route type that can just give the controller a list of directories, and I can sort out the relationships in the controller? It would be a bit more work, but I could deal with that.
Upvotes: 2
Views: 1342
Reputation: 47375
Unfortunately, ASP.NET routing in the context of ASP.NET MVC doesn't work too well with an arbitrary number of url segments like folder1/folder2/folderN. You could do a catchall route:
...MapRoute(null, "{*folders}",
new { controller = "DirectoryStructure", action = "Parse"}
);
DirectoryStructureController.Parse action method would then look like this:
public ActionResult Parse(string folders) {...}
You could split the string to determine which content to return, whether to redirect to another action, or return HttpNotFound.
Update
The problem with the approach above is that all of your URL's would be routed to the DirectoryStructureController. You could eliminate this by using a prefix like filesystem:
...MapRoute(null, "file-system/{*folders}",
new { controller = "DirectoryStructure", action = "Parse"}
);
Then, requests would not be routed to this controller unless they were prefixed with /file-system like so:
http://mysite.com/dirA/dirB/dirN/ <-- will not be routed
http://mysite.com/file-system/dirA/dirB/dirN/ <-- will be routed
Update2
I also just realized this route would only work for resolving incoming URL's. You wouldn't be able to use it to generate an outgoing URL.
Upvotes: 3