View components are new in ASP.NET Core. You can think of them as replacements for partial views (which are still around) and the RenderAction
method for returning child actions (which are gone). No more being tied to controllers; they are reusable because they can be loaded from external assemblies (that is, not the assembly of the web app) and they are better suited than partial views to render complex HTML.
View components are discovered in one of the following ways:
- Inheriting from the
ViewComponent
class - Adding a
[ViewComponent]
attribute - Adding the
ViewComponent
suffix to a class
Most likely, you will be inheriting from ViewComponent
, as this class offers a couple of useful methods. View components can be loaded from external assemblies, if the web app has references to them or they are registered as application parts:
services.AddMvc() .ConfigureApplicationPartManager(options => { options.ApplicationParts.Add(new AssemblyPart...