You can create a view component using any of the following methods:
- Create a class by deriving one from the ViewComponent attribute
- Enhance a class with the [ViewComponent] attribute or derive it from the class that has the [ViewComponent] attribute
- Use the convention by creating a class that ends with a suffix ViewComponent attribute
Whichever option you choose, the view component should be a public, non-nested, and non-abstract class.
As with controllers, you can use dependency injection (via a constructor) in the ViewComponent attribute too. As the ViewComponent attribute is separate from the controller life cycle, you may not be able to use the action filters in ViewComponents.
There is a method called Invoke (or InvokeAsync, the asynchronous equivalent of Invoke), that will return the IComponentViewResultinterface. This method is similar to the action method of the controller that will return the view.