Creating a View Component

You can create a view component using any of the following methods:

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.