April 12, 2021 by Anuraj Estimated read time : 3 mins
This article will discuss about implementing a tag helper in ASP.NET Core MVC which helps you apply class to your bootstrap menu based on route. By default asp.net core mvc comes with Bootstrap 4 as the UI framework. In Bootstrap, the active menu can be high lighted using active class. This asp.net core tag helper will help you to apply the active class based on the route. Here is the implementation.
In this tag helper, the controller and action methods are read from the route data object. And once it receive the action and/or controller and compare it with current action and controller class - these classes available since you’re inheriting from the AnchorTagHelper. And once it is found - aspnet core reads the existing class and remove it from output object. And adding it back again.
And it can be used like this.
Also you need to make sure you’re including the assembly name in the _ViewImports.cshtml file. Otherwise your tag helper might not work. Here is the output working in one of the ASP.NET Core application.
This way you can implement the active tag helper in aspnet core.