tags:

views:

369

answers:

2

If I do this in my view

<% Html.RenderAction("RenderAdminMenu", "Shared"); %>

and then have this in controller action RenderAdminMenu:

RouteData rd = route.GetRouteData(Url.RequestContext.HttpContext);

or

RouteData rd = route.GetRouteData(this.HttpContext);

then RouteData returned (rd) is always null.

How come?

+1  A: 

Try using HttpContext.Current instead of Url.RequestContext.HttpContext, I am not very sure if this is the problem.

Thanks

Mahesh Velaga
I was seeing this HttpContext.Current everywhere however when I try to access it in my controllers or view it is not there. I type HttpContext. to list it and there is no Current property in it.
mare
You are trying to use System.Web.Mvc.Controllers.HtttpContext, there is another System.Web.HttpContext, try that and I think that should be fine .. Thanks
Mahesh Velaga
+1  A: 

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; }
    }
}
mare