I am trying to create a dynamic menu (stored in a database) that appears on all pages of a web application. Using Google, I found it better to make the menu look as part of the Master View (_Layout.cshtml). And because of this, each controller action method must contain data with a menu model. To avoid code duplication, I found a solution to create a basic controller and provide data using its constructor:
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/passing-data-to-view-master-pages-cs
In addition, I am trying to use the features of async / await, and my PageService menu (menu) uses ToListAsync () to retrieve data from the database. So now I have a problem that the BaseController constructor has an async method:
public class BaseController : AsyncController, IBaseController { private readonly IPageService _pageService; public BaseController(IPageService pageService) { _pageService = pageService; SetBaseViewModelAsync(); } private async Task SetBaseViewModelAsync() { ViewData["Pages"] = await _pageService.GetAllAsync(); } }
I know this is BAD CODE , but I do not know how to properly design this situation. Maybe there is another better way to create a dynamic menu or another way to get data asynchronously?
Also, I found this article, but I donโt know if I can apply its solutions, because I donโt know if I can handle the creation of the controller instance:
http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html
source share