[英]ViewComponent not found in Asp.NET Core 2.2 using Razor Pages
I have created a very simple view component ( /ViewComponents/MenuViewComponent.cs
):我创建了一个非常简单的视图组件( /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
):视图部分在这里( /Pages/Components/Menu/Default.cshtml
):
<div>Menu</div>
I am invoking the Menu from the _Layout.cshtml
file like this:我正在从_Layout.cshtml
文件调用菜单,如下所示:
@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 ASP.NET Core 中的参考视图组件
and also when calling the component just use the name without the suffix并且在调用组件时只需使用不带后缀的名称
@await Component.InvokeAsync("Menu")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.