[英]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 必须只有一个名为InvokeAsync或Invoke的公共方法
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.