Skip to content

Instantly share code, notes, and snippets.

@andrexx
Last active August 29, 2015 14:16
Show Gist options
  • Save andrexx/24a985520b1e3acb2c2e to your computer and use it in GitHub Desktop.
Save andrexx/24a985520b1e3acb2c2e to your computer and use it in GitHub Desktop.
Display MVC routes
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