Last active
August 29, 2015 14:16
-
-
Save andrexx/24a985520b1e3acb2c2e to your computer and use it in GitHub Desktop.
Display MVC routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Test() | |
{ | |
var sb = new StringBuilder(); | |
using (RouteTable.Routes.GetReadLock()) | |
{ | |
foreach (var routeBase in RouteTable.Routes) | |
{ | |
if (routeBase.GetType().ToString() == "System.Web.Mvc.Routing.LinkGenerationRoute") | |
{ | |
continue; | |
} | |
if (routeBase.GetType().ToString() == "System.Web.Mvc.Routing.RouteCollectionRoute") | |
{ | |
// This catches any routing that has been defined using Attribute Based Routing | |
// System.Web.Http.Routing.RouteCollectionRoute is a collection of HttpRoutes | |
var subRoutes = | |
routeBase.GetType() | |
.GetField("_subRoutes", BindingFlags.NonPublic | BindingFlags.Instance) | |
.GetValue(routeBase); | |
var routes = | |
(IList<Route>) | |
subRoutes.GetType() | |
.GetField("_routes", BindingFlags.NonPublic | BindingFlags.Instance) | |
.GetValue(subRoutes); | |
for (var i = 0; i < routes.Count; i++) | |
{ | |
var route = routes[i]; | |
if (route != null) | |
{ | |
sb.AppendLine(route.Url); | |
} | |
} | |
} | |
else | |
{ | |
var route = routeBase as Route; | |
if (route != null) | |
{ | |
sb.AppendLine(route.Url); | |
} | |
else | |
{ | |
sb.AppendLine(routeBase.ToString()); | |
} | |
} | |
} | |
} | |
return new ContentResult { Content = sb.ToString() }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment