Adding menu items
Menus are UI components that make it easy for users to find and navigate to specific or even undiscovered content and features in the store. Building on the previous tutorial, you will add a menu item to the Admin menu.
Adding a menu
First, you create the AdminMenu.cs class in the root folder of your module.
namespace MyOrg.HelloWorld
{
public class AdminMenu
{
}
}Implement the AdminMenuProvider
AdminMenuProviderThis class inherits from the abstract base class AdminMenuProvider and overrides the BuildMenuCore method.
public class AdminMenu : AdminMenuProvider
{
protected override void BuildMenuCore(TreeNode<MenuItem> modulesNode)
{
}
}Create a menu item
Next, you create a MenuItem using the ToBuilder() method.
ResKeyis a reference to the resource in your localization files.Iconadds a menu icon.Actioncreates a route to your action, defined in the specified controller.This menu will simply point to your
Configuremethod from theHelloWorldAdminController.
Add a localization string
Add a new resource to your localization to display the menu text.
Create tree nodes
Create a TreeNode menuNode from myMenuItem.
Get a reference node from modulesNode. This example uses the menu id settings.
Insert menuNode after refNode.
Now you should see a menu item in the admin configuration menu. Your code should look something like this:
Adding a submenu
Adding a submenu is very easy. First, you add a menu, just as you did above. The only difference is that you don't give the menu item an action.
Then create another menu item and specify the action.
Add localization strings
Add two new resources to your localization.
Create tree nodes
Again, create a TreeNode for each menu item.
Then use menuNode to insert and append the new menu items.
Your final code should look like this:
Conclusion
In this tutorial, you created a menu item, added it to the Admin menu, and added a submenu.
Last updated
Was this helpful?