I have created a very simple view component (/ViewComponents/MenuViewComponent.cs
):
namespace MySite.ViewComponents
{
public class MenuViewComponent : ViewComponent
{
public Task<IViewComponentResult> InvokeAsync()
{
return Task.FromResult((IViewComponentResult)View("Default"));
}
}
}
The view part is here (/Pages/Components/Menu/Default.cshtml
):
<div>Menu</div>
I am invoking the Menu from the _Layout.cshtml
file like this:
@await Component.InvokeAsync("MenuViewComponent");
When viewing a page I get the following error message:
InvalidOperationException: A view component named 'MenuViewComponent' 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 ending with the 'ViewComponent' suffix.
A view component must not be decorated with 'NonViewComponentAttribute'.
Question:
What am I missing to get this working?
Make sure component view is in the search path as described in the following
Reference View components in ASP.NET Core
and also when calling the component just use the name without the suffix
@await Component.InvokeAsync("Menu")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With