View components
View components are a new feature introduced in ASP.NET Core, they are almost similar to Partial Views but is more powerful. When you use Partial Views, you have dependency over the Controller. However, when you use the ViewComponent
attribute, you do not have to depend on the Controller, so we will establish separation of concerns and have better testability. Even though the existing Partial View HTML helper is still supported, it is preferable to use the View component whenever you want to show a reusable piece of information when you are using .NET Core.
Creating a View component
You can create a ViewComponent
using any of the following:
Create a class by deriving from the
ViewComponent
attributeDecorate a class with the
[ViewComponent]
attribute or derive it from the class that has the[ViewComponent]
attributeYou can use the convention by creating a class that ends with a suffix
ViewComponent
attribute
Whatever option you choose, this ViewComponent
should be public, non...