It\'s a simple question. How did stackoverflow do their menu in Asp.net MVC, with highlight on what page we are on.
1.First create extention
public class Extention { public static Dictionary GetDictionary() { Dictionary urls = new Dictionary(); urls.Add(new Menu { Controller = "AppHome", Action = "Index" }, new Menu { Controller = "AppHome", Action = "Index" }); urls.Add(new Menu { Controller = "Home", Action = "Index" }, new Menu { Controller = "Home", Action = "Index" }); return urls; } } public static class HtmlExtensions { public static MvcHtmlString ActionMenu(this HtmlHelper helper,String linkText,string actionName,String controllerName) { var tag= new TagBuilder("li"); if(helper.ViewContext.RequestContext.IsCurrentRoute(null,controllerName,actionName)|| helper.ViewContext.RequestContext.IsParentRoute(controllerName,actionName)) { tag.AddCssClass("active"); } else { tag.AddCssClass("inactive"); } tag.InnerHtml = helper.ActionLink(linkText, actionName, controllerName).ToString(); return MvcHtmlString.Create(tag.ToString()); } } public static class RequestExtentions { public static bool IsCurrentRoute(this RequestContext context, String areaName) { return context.IsCurrentRoute(areaName, null, null); } public static bool IsCurrentRoute(this RequestContext context, String areaName, String controllerName) { return context.IsCurrentRoute(areaName, controllerName, null); } public static bool IsCurrentRoute(this RequestContext context, String areaName, String controllerName, params String[] actionNames) { var routeData = context.RouteData; var routeArea = routeData.DataTokens["area"] as String; var current = false; if (((String.IsNullOrEmpty(routeArea) && String.IsNullOrEmpty(areaName)) || (routeArea == areaName)) && ((String.IsNullOrEmpty(controllerName)) || (routeData.GetRequiredString("controller") == controllerName)) && ((actionNames == null) || actionNames.Contains(routeData.GetRequiredString("action")))) { current = true; } return current; } public static bool IsParentRoute(this RequestContext context, String controller, String action) { var routeData = context.RouteData; Menu returnUrl = null; Menu requestUrl = new Menu { Action = routeData.GetRequiredString("action"), Controller = routeData.GetRequiredString("controller") }; Menu linkUrl = new Menu { Action = action, Controller = controller }; var urls = Extention.GetDictionary(); urls.TryGetValue(requestUrl, out returnUrl); if (returnUrl != null && returnUrl.Equals(linkUrl)) return true; else return false; ; } }