MVC 3 菜单生成, 以及权限控制
public class MenuFactory { public static MvcHtmlString Bind(HttpContext context) { StringBuilder builder = new StringBuilder(); if (context.Request.IsAuthenticated) { string welcomeAndSignOut = "<div id=\"welcome\"><a href=\"/Home/Index\">Welcome " + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(context.User.Identity.Name) + "</a> | <a href=\"/Home/Index/-1\" title=\"Sign Out\">Sign Out</a> </div><hr/>"; Webs.IWebMenu menu = MenuContext.Creator(context); if (menu != null && menu.ContextMenuType!= Webs.MenuType.Null) { string key = string.Format("{0}_{1}", menu.ContextMenuType.ToString(), context.User.Identity.Name.ToUpper()); var menuString = Common.CacheManager.GetCache(key); if (menuString == null) { menuString = welcomeAndSignOut + menu.BindMenuString(); Common.CacheManager.SetCache(key, menuString, null, TimeSpan.FromMinutes(30)); } return MvcHtmlString.Create(menuString.ToString()); } return MvcHtmlString.Create(welcomeAndSignOut); } return MvcHtmlString.Create(""); } }
internal class MenuItem { public List<MenuItem> MenuItems = new List<MenuItem>(); public string Name { get; set; } public string Cls { get; set; } public string Url { get; set; } public bool IsAllow { get; set; } public string Bind() { StringBuilder builder = new StringBuilder(); if (IsAllow) { builder.AppendLine(string.Format("<li><a href=\"{0}\"{1}>{2}</a></li>", Url,string.IsNullOrEmpty(Cls) ? "" : string.Format("class=\"{0}\"", Cls),Name)); builder.AppendLine(BindMenuItems); builder.AppendLine("</li>"); } return builder.ToString(); } string BindMenuItems { get { StringBuilder builder = new StringBuilder(); if (MenuItems != null && MenuItems.Count > 0) { builder.AppendLine("<ul>"); foreach (var sub in MenuItems) { if (sub.IsAllow) { builder.AppendLine(string.Format("<li><a href=\"{0}\"{1}>{2}</a></li>", sub.Url,string.IsNullOrEmpty(sub.Cls) ? "" : string.Format("class=\"{0}\"", sub.Cls),sub.Name)); } } builder.AppendLine("</ul>"); } return builder.ToString(); } } }
internal class MenuRoot { public List<MenuItem> MenuItems = new List<MenuItem>(); public string Bind() { StringBuilder buidler = new StringBuilder(); buidler.AppendLine("<ul id=\"main-nav\">"); MenuItems.ForEach(menu => { buidler.AppendLine(menu.Bind()); }); buidler.AppendLine("</ul>"); return buidler.ToString(); } }
internal interface IWebMenu { MenuType ContextMenuType { get; } MenuRoot ConstuctMenuRoot(); void DisableMenuItem(Menus.MenuRoot root); string BindMenuString(); }
internal enum MenuType { Netscan, Null }
internal class Netscan : IWebMenu { HttpContext context_ = null; public Netscan(HttpContext context) { context_ = context; } public MenuRoot ConstuctMenuRoot() { MenuRoot root = new MenuRoot(); MenuItem home = new MenuItem { Name = "Home", Url = "#", Cls = "nav-top-item current", IsAllow = true }; home.MenuItems.Add(new MenuItem { Name = "HoldOn", Url = Common.Url(context_, "Home", "Index", 0), IsAllow = true }); home.MenuItems.Add(new MenuItem { Name = "InProcess", Url = Common.Url(context_, "Home", "Index", 1), IsAllow = true }); home.MenuItems.Add(new MenuItem { Name = "Email", Url = Common.Url(context_, "Home", "Index", 2), IsAllow = true }); home.MenuItems.Add(new MenuItem { Name = "Completed", Url = Common.Url(context_, "Home", "Index", 3), IsAllow = true }); MenuItem report = new MenuItem { Name = "Report", Url = "#", Cls = "nav-top-item", IsAllow = true }; report.MenuItems.Add(new MenuItem { Name = "MY", Url = Common.Url(context_, "Home", "Index", 4), IsAllow = true }); report.MenuItems.Add(new MenuItem { Name = "IB", Url = Common.Url(context_, "Home", "Index", 5), IsAllow = true }); root.MenuItems.Add(home); root.MenuItems.Add(report); return root; } public string BindMenuString() { if (ContextMenuType == MenuType.Null) return ""; MenuRoot root =ConstuctMenuRoot(); DisableMenuItem(root); return root.Bind(); } public void DisableMenuItem(MenuRoot root) { if (Roles.IsUserInRole("NS-User")) { var ibd_menu = root.MenuItems.FirstOrDefault(n => n.Name == "Report") .MenuItems.Where(n => n.Name != "MY"); ibd_menu.ToList().ForEach(m=>m.IsAllow = false); } } public MenuType ContextMenuType { get { if (Roles.IsUserInRole("NS-Admin") || Roles.IsUserInRole("NS-User")) return MenuType.Netscan; return MenuType.Null; } }
internal class MenuContext { public static Webs.IWebMenu Creator(HttpContext context) { Webs.IWebMenu menu= new Webs.Netscan(context); //todo: if the menu.ContextMenuType is MenuType.Null, then you can continue to add other class is implemented Webs.IWebMenu. // ... if(menu.ContextMenuType == MenuType.Null) return null; else return menu; } }
<div id="sidebar-wrapper"> <div id="profile-links"> <br /> Netscan Web System </div> @MenuCreator.MenuFactory.Bind(this.ApplicationInstance.Context) </div>