简体   繁体   English

使用 Razor Pages 在 Asp.NET Core 2.2 中找不到 ViewComponent

[英]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.

相关问题 在razor,asp.net Core 2.2页面之间传递值 - Passage of values ​between pages razor, asp.net Core 2.2 在带有Razor Pages Viewcomponent .Net Core 2.1的选项卡中使用多个模型 - Using multiple models in tabs with Razor Pages Viewcomponent .Net Core 2.1 ASP.NET Core 2.2 Razor页面MVVM查询AspNetUsers和AspNetRoles表 - ASP.NET Core 2.2 Razor Pages MVVM Query the AspNetUsers and AspNetRoles Tables ASP.NET Core 2.2 Razor Pages 为什么 OnPost() 有效,而 OnPostAsync() 无效? - ASP.NET Core 2.2 Razor Pages why does OnPost() work, but OnPostAsync() doesn't? 如何在asp.net核心剃须刀页面2.2中设置日期格式和区域性 - How to set date formats and culture in asp.net core razor pages 2.2 带ASP.NET Core 2.2的Razor页面中的嵌套/子区域 - Nested/Sub-Areas in Razor Pages w/ASP.NET Core 2.2 IIS 部署不能在 asp.net core 2.2 razor 页面中包含 wwwroot 文件夹的文件 - IIS deployment cannot contains files for wwwroot folder in asp.net core 2.2 razor pages 如何将ASP.NET Core 2.2 Razor页面与MySql数据库连接? - How to connect ASP.NET Core 2.2 Razor pages with MySql database? 在 asp.net 核心 razor 页面的部分视图中使用 forms - Using forms in partial views in asp.net core razor pages 使用ASP.NET CORE 2 Razor页面的处理程序错误 - Handlers error using ASP.NET CORE 2 Razor Pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM