Create an interface ‘INavData’ which holds values for the menu and Create a class ‘NavData’ from the interface with all those properties.

  1. public interface INavData
  2. {
  3. string ImageFileNameWithNoExtn
  4. {
  5. get;
  6. set;
  7. }
  8. string AreaName
  9. {
  10. get;
  11. set;
  12. }
  13. bool IsActive
  14. {
  15. get;
  16. set;
  17. }
  18. bool IsDivider
  19. {
  20. get;
  21. set;
  22. }
  23. bool IsHeader
  24. {
  25. get;
  26. set;
  27. }
  28. string Text
  29. {
  30. get;
  31. set;
  32. }
  33. string ContorllerName
  34. {
  35. get;
  36. set;
  37. }
  38. string ActionName
  39. {
  40. get;
  41. set;
  42. }
  43. string AlternateUrl
  44. {
  45. get;
  46. set;
  47. }
  48. bool OpenNewWindow
  49. {
  50. get;
  51. set;
  52. }
  53. IEnumerable < INavData > Children
  54. {
  55. get;
  56. set;
  57. }
  58. }
Create a class ‘MenuBuild’ and a method ‘RenderMenu’ to hold the role based menu generation algorithm.
  1. public static class MenuBuild
  2. {
  3. public static IEnumerable < INavData > RenderMenu(IPrinciple User, Func < ActionResult, string > urlAction)
  4. {
  5. List < INavData > ComponentMenuList = new List < INavData > ();
  6. if (User.IsInRole("Authenticated_User"))
  7. {
  8. NavData Home = new NavData
  9. {
  10. IsActive = true,
  11. AlternateUrl = urlAction(MVC.Home.Actions.Index()),
  12. ActionName = MVC.Home.ActionNames.Index,
  13. ControllerName = MVC.Home.Name,
  14. Text = "Home"
  15. }
  16. ComponentMenuList.Add(Home);
  17. }
  18. List < INavData > subMenu1 = new List < INavData > ();
  19. if (IPrincipleExtensions.IsInAnyRole(User.IsInRole, ComponentViewer.ComponentViewerRoles))
  20. {
  21. NavData newCompMenu1 = new NavData
  22. {
  23. IsActive = true,
  24. AlternateUrl = urlAction(MVC.Comp.Actions.Index()),
  25. ActionName = MVC.Comp.ActionNames.Index,
  26. ControllerName = MVC.Comp.Name,
  27. Text = "Comp"
  28. }
  29. subMenu1.Add(newCompMenu1);
  30. }
  31. if (IPrincipalExtensions.IsInAnyRole(User.IsInRole, ComponentViewer.ComponentViewerRoles))
  32. {
  33. NavData newCompMenu2 = new NavData
  34. {
  35. IsActive = true,
  36. AlternateUrl = urlAction(MVC.Comp.Actions.Edit()),
  37. ActionName = MVC.Comp.ActionNames.Edit,
  38. ControllerName = MVC.Comp.Name,
  39. Text = "Comp Edit"
  40. }
  41. subMenu1.Add(newCompMenu2);
  42. }
  43. if (subMenu.Count > 0)
  44. {
  45. List < INavData > subMenu1Child = new List < INavData > ();
  46. NavData subMenu1Header = new NavData
  47. {
  48. Text = "New",
  49. Children = subMenu1Child
  50. }
  51. ComponentMenuList.Add(subMenu1Child);
  52. }
  53. }
  54. }
Write an extension class to the ‘IPrincipal’ with ‘IsInAnyRole’ method to loop the roles.
  1. public static class IPrincipalExtensions
  2. {
  3. public static bool IsInAnyRole(Func<string,bool> IsInRole, IEnumerable<string> roles)
  4. {
  5. foreach(string role in roles)
  6. if(IsInRole(role)) return true;
  7. return false;
  8. }
  9. }