1) To access information about the current route:
RouteValueDictionary values = Url.RequestContext.RouteData.Values;
then go thru the values like this:
foreach (KeyValuePair<string, object> pair in values)
{
if (pair.Key.Equals("slug", StringComparison.InvariantCultureIgnoreCase))
2) If, however, we need access to the whole route collection, enumerate them or just do whatever we want (like build up a navigation menu from our routes, which I did) we do it like this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="pending.MVC" %>
<% if (RouteTable.Routes.Count > 0 && Request.IsAuthenticated)
{ %>
<div id="AdminMenu">
<ul>
<%
foreach (NamedTypedRoute r in RouteTable.Routes)
{
if (r.Type == RouteType.Admin)
{
// check if current view is opened in modal mode; if so, correct the admin menu links so
// they open (remain) modal too
bool modal = false;
string append = string.Empty;
if (Request.QueryString["modal"] != null)
modal = bool.Parse(Request.QueryString["modal"]);
if (modal)
append = "?modal=true";
%>
<li><a href="<%=Url.RouteUrl(r.Name) + append%>"><%=r.Name%></a></li>
<%
}
}
%></ul>
</div>
<% } %>
For this to work you would also need a special NamedTypedRoute class:
public class NamedTypedRoute : Route
{
private string _name;
public RouteType Type { get; set; }
public NamedTypedRoute(string name, RouteType type, string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
_name = name;
Type = type;
}
public NamedTypedRoute(string name, RouteType type, string url, RouteValueDictionary defaults,
RouteValueDictionary constraints, IRouteHandler routeHandler)
: base(url, defaults, constraints, routeHandler)
{
_name = name;
Type = type;
}
public NamedTypedRoute(string name, RouteType type, string url, RouteValueDictionary defaults,
RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
: base(url, defaults, constraints, dataTokens, routeHandler)
{
_name = name;
Type = type;
}
public string Name
{
get { return _name; }
}
}