2

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.

2
  • How deep can your directory structure go? Is there a limit? Commented Dec 27, 2011 at 15:12
  • No limit is coded in anywhere. Realistically maybe 5-10 levels maximum. Commented Dec 27, 2011 at 15:16

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I was gonna ask about generating links. I would probably need a helper to generate one from a folder. I think the first would work, it would just be the last of my routes so anything above it would match, and the catch-all would be just that.
Yes precisely. I was going to mention you could do outgoings with a HtmlHelper, but there was nothing in your question about incoming vs. outgoing routes. Also there could be other approaches depending on your project -- you could put the URL's into a viewmodel for example. If you put this as your last route definition instead of using a prefixer, be careful that none of your root folders match any of your controller names. Otherwise, when the user tries to browse to the Home folder, they will get your HomeController.Index action method.
ouch. A prefix won't be super bad, and it will protect me from those nasty edge cases.

Your Answer

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.