I am attempting to load a view component, and no matter where I put it, it does not load. Instead I get this error. I have tried all of the default locations under Page listed as where dot net core looks automatically. Just when I was feeling like I was finally getting the hang of Core, I cannot resolve something this simple.(smile)
InvalidOperationException: A view component named 'MenuView' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with 'ViewComponentAttribute' or have a class name
<div> @(await Component.InvokeAsync("MenuViewComponent"));</div>
It doesn't get as far as the actual view component, but I do have some test code in place, for when it does.
public class MenuViewComponent:ViewComponent { public async Task<IViewComponentResult> InvokeAsync() { var items = new List<string>(); return View(items); } }
I also tried these directions and got the same error.
Create the Views/Shared/Components folder. This folder must be named Components.
Create the Views/Shared/Components/PriorityList folder. This folder name must match the name of the view component class, or the name of the class minus the suffix (if we followed convention and used the ViewComponent suffix in the class name). If you used the
ViewComponent
attribute, the class name would need to match the attribute designation.Create a Views/Shared/Components/PriorityList/Default.cshtml Razor view:
I found a solution, but I cannot believe it is the right way. I decorated the class with [ViewComponent(Name = "Menu")] and then it worked.
I am sure there is some better way to do it rather than the long chain of directories, the decorator etc.