Olivier Muhring

Olivier Muhring

  • NA
  • 150
  • 10k

Loading a menu structure on every razor page in asp .NET Core

Aug 28 2020 10:20 AM
After user authentication I'm supposed to change the user menu based on the roles of the user.
 
To do this, I created a service which fetched the menu the user is expected to see:
  1. public List<MenuStructure> LoadCompleteMenuRole(string role)    
  2. {    
  3.     List<MenuStructure> resultList = null;  
  4.     var menuHeaders = LoadMenuHeadersForTheGivenRole(role);    
  5.     if (menuHeaders != null && menuHeaders.Count > 0)    
  6.     {    
  7.         resultList = new List<MenuStructure>();  
  8.         foreach (var menuHeader in menuHeaders)    
  9.         {    
  10.             var menuStructure = new MenuStructure    
  11.             {    
  12.                 MenuName = menuHeader.HeaderName,    
  13.                 Caption = menuHeader.Caption    
  14.             };  
  15.             var menuDetails = LoadMenuDetailsForTheGivenHeader(menuHeader.Id);  
  16.             if (menuDetails != null && menuDetails.Count > 0)    
  17.             {    
  18.                 menuStructure.MenuElements = new List<MenuElement>();    
  19.                 foreach (var menuDetail in menuDetails)    
  20.                 {    
  21.                     var menuElement = new MenuElement    
  22.                     {    
  23.                         ElementName = menuDetail.DetailName,    
  24.                         ElementCaption = menuDetail.Caption,    
  25.                         Destination = menuDetail.Destination    
  26.                     };  
  27.                     menuStructure.MenuElements.Add(menuElement);    
  28.                 }    
  29.             }   
  30.             resultList.Add(menuStructure);    
  31.         }    
  32.     }   
  33.     return resultList;    
  34. }    
My problem? Where do I implement the service so the menus get loaded in every razor page I create?

Answers (1)