Open Closed

Need to trigger a event when a tenant logins and add a dynamic menu #6604


User avatar
0
Sraman created
  • ABP Framework version: v8.0.0
  • UI Type: MVC
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

Looking to trigger an event when a tenant login and load a dynamic menu using cache or any other custom method instead of IMenuContributor. So basically, looking for two things,

  1. Event handler which invokes when a tenant logins
  2. Based on the tenant Id want to perform a external api get call and save those data to an existing DB.
  3. Finally want to load the dynamic menu with the data pulled from get api call.

Need a proper way to tackle this. Not sure using IMenuContributor to load a dynamic menu is a good practice.

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

3 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

    You can consider using distributed cache to store the dynamic menus.

    For example:

    public class MyMenuContributor : IMenuContributor
    {
        private readonly IDistributedCache<DynamicMenusCacheItem> _menusCache;
         
        public MyMenuContributor(IConfiguration configuration)
        {
            _configuration = configuration;
        }
    
        public async Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            if (context.Menu.Name == StandardMenus.Main)
            {
                await ConfigureMainMenuAsync(context);
            }
        }
    
        private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
            var dynamicMenus = await _menusCache.GetOrAddAsync(
                    nameof(DynamicMenusCacheItem), //Cache key
                    async () => await GetFromRemoteAPI(),
                    () => new DistributedCacheEntryOptions
                    {
                        AbsoluteExpiration = DateTimeOffset.Now.AddHours(1)
                    }
                );
                
            //...
        }
        
        private Task<DynamicMenusCacheItem> GetFromRemoteAPI()
        {
            //...
        }
    }
    
    public class DynamicMenusCacheItem
    {
       ....
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    Sraman created

    Hi, And also how can I trigger an external api call and save the response to DB. Is it a good practice to do that from menu contributor?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

    I think there is no problem, you can abstract some services if you need to. for example:

    • Create IDynamicMenuStore interface
    • Create DynamicMenuStore to implement IDynamicMenuStore interface
    public interface IDynamicMenuStore
    {
         Task<List<DynamicMenuItems>> GetMenuAsync();
    }
    
    public class DynamicMenuStore: IDynamicMenuStore, ITransientDependency
    {
         public Task<List<DynamicMenuItems>> GetMenuAsync()
         {
             ....
         }
    }
    
    public class MyMenuContributor : IMenuContributor
    {
        private readonly IDynamicMenuStore _dynamicMenuStore;
         
        public MyMenuContributor(IDynamicMenuStore dynamicMenuStore)
        {
            _dynamicMenuStore = dynamicMenuStore;
        }
    
        public async Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            if (context.Menu.Name == StandardMenus.Main)
            {
                await ConfigureMainMenuAsync(context);
            }
        }
    
        private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
            var dynamicMenus = await _dynamicMenuStore.GetMenuAsync();
                
            //...
        }
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 23, 2026, 09:57
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.