Search code examples
c#asp.net-core.net-coreasp.net-core-mvcasp.net-core-viewcomponent

Remove the naming convention rule for finding ViewComponents


As per the documentation the following rules determine whether a class is a ViewComponent:

  1. Deriving from ViewComponent
  2. Decorating a class with the [ViewComponent] attribute, or deriving from a class with the [ViewComponent] attribute
  3. Creating a class where the name ends with the suffix ViewComponent

I am adding View components to an existing project, and in this project there are classes that are Suffixed "viewComponent" e.g. ReviewComponent.

When I call IViewComponentSelector.SelectComponent an exception is being thrown because the runtime cannot find an InvokeAsync method on these classes.

So my question is, is it possible to disable or remove rule #3 so that my existing classes aren't mistaken for View components, or is there a way to flag these classes as "not view components"?


Solution

  • You can use the NonViewComponent attribute on the classes in question, if that's a workable solution for you:

    [NonViewComponent]
    public class ReviewComponent
    {
        // ...