简体   繁体   English

ViewComponent 必须有一个名为“InvokeAsync”或“Invoke”的公共方法 - ASP.NET CORE (.NET 6)

[英]ViewComponent must have one public method named 'InvokeAsync' or 'Invoke' - ASP.NET CORE (.NET 6)

I am now designing a website using .NET6 and unlike older versions of ASP.NET Core, this version doesn't have methods like @{Html.RenderPartial("_ViewNamePartial")} so I'm using ViewComponents but when I try to render it, it throws an error:我现在正在使用 .NET6 设计一个网站,与旧版本的 ASP.NET Core 不同,这个版本没有像@{Html.RenderPartial("_ViewNamePartial")}这样的方法,所以我正在使用 ViewComponents 但是当我尝试渲染它时,它抛出一个错误:

InvalidOperationException: View component 'aquaWeb.ViewComponents.ProductCardViewComponent' must have exactly one public method named 'InvokeAsync' or 'Invoke'. InvalidOperationException:视图组件“aquaWeb.ViewComponents.ProductCardViewComponent”必须只有一个名为“InvokeAsync”或“Invoke”的公共方法。

Here is my code:这是我的代码:

Parent page that calls ViewComponent:调用 ViewComponent 的父页面:

<html>
    <body>
        <div class="row">
            <div class="col-md-9">
                @await Component.InvokeAsync("ProductCard");
            </div>
        </div>
    </body>
</html>

View Component:查看组件:

public class ProductCardViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync()
        {
            return await Task.FromResult((IViewComponentResult)View());
        }
        public IViewComponentResult Invoke()
        {
            return View();
        }
    }

So apparently the error was:所以显然错误是:

ViewComponent must have ONLY one public method named InvokeAsync or Invoke ViewComponent 必须只有一个名为InvokeAsyncInvoke的公共方法

ViewComponent wants exactly just one method, not much or less. ViewComponent 只需要一种方法,不多也不少。

Going to read more careful next time...下次要仔细阅读...

I changed my ViewComponent class from this:我从这里更改了我的 ViewComponent class:

public class ProductCardViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync()
        {
            return await Task.FromResult((IViewComponentResult)View());
        }
        public IViewComponentResult Invoke()
        {
            return View();
        }
    }

To this:对此:

public class ProductCardViewComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync()
        {
            return await Task.FromResult((IViewComponentResult)View());
        }
    }

and it's working pretty well它运行良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法识别 ASP.NET Core ViewComponent 的 Invoke() 方法调用 - ASP.NET Core ViewComponent's Invoke() method call not recognized Asp.Net Core 将 ViewComponent 渲染为变量 - Asp.Net Core render ViewComponent to variable ASP.NET Core ViewComponent调用:InvalidCastException - ASP.NET Core ViewComponent Invocation: InvalidCastException 在继承的 ViewComponent .net core 5.0 中覆盖 Invoke 方法 - Override Invoke method in an inherited ViewComponent .net core 5.0 Asp.net Core Web API中间件错误`“调用”方法的第一个参数必须为“ HttpContext”类型 - Asp.net Core web api middleware Error `The 'Invoke' method's first argument must be of type 'HttpContext'` 在 ASP.NET CORE MVC 中使用 CRUD 操作的 ViewComponent - ViewComponent With CRUD Operations in ASP.NET CORE MVC 如何在Asp.net核心中的ViewComponent之间共享ViewData - How share ViewData between ViewComponent in Asp.net core 使用 Razor Pages 在 Asp.NET Core 2.2 中找不到 ViewComponent - ViewComponent not found in Asp.NET Core 2.2 using Razor Pages 如何将 ModelExpression 绑定到 ASP.NET Core 中的 ViewComponent? - How to bind a ModelExpression to a ViewComponent in ASP.NET Core? ASP.NET Core使用ViewComponent跟踪侧栏的活动页面 - ASP.NET Core track active Page for sidebar using ViewComponent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM