Think In Geek

In geek we trust

Routing a hiearchical path in ASP.NET MVC

If you wonder how to make a GitHub (and other websites) like routes to access to hierarchical paths or files, here’s the way to do it in ASP.NET MVC. GitHub is a GIT hosting service, and allows you to browse the repositories. When doing so, it uses a path as a routing parameter, as seen in this URL: This includes slashes and so to represent the directories, and is a parameter that depends on the file location inside the repository. A route like this can be done in ASP.NET using the called catch-all parameter.

The catch-all parameter allows you to use a wildcard on a route, so it takes everything after a given route as a single parameter. You can find the explanation of this feature in the ASP.NET Routing MASDN help page.

All you need to do to make a route like the one in the example to work is add this code to your Global.asax file, in the RegisterRoutes method:

    new { controller = "Blob", action = "GetContents" }

This will pass the controller Blob a parameter called path that will contain the parameter you want (in the example that would be master/Core/Models/Commit.cs. All you have to do now is use this parameter as you wish so you can access the desired file and show it’s contents on a web page.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn


Leave a Reply

Your email address will not be published. Required fields are marked *