什么是调用方法?
下面是我的 viewcomponent()
方法。
public IViewComponentResult Invoke()
{
ViewBag.SelectedCategory = RouteData?.Values["category"];
return
View(repository.Products.Select(x=>x.Category).Distinct().OrderBy(x=>x));
}
这是匿名函数吗?
这个函数的名称是什么?
我尝试将"Invoke" 更改为"func",但没有语法错误,但发生了运行时错误。
是否可以在单个 View 组件类中使用多个名称为“Invoke”并接收不同参数的方法。
我知道 View 组件用于为此呈现部分 View 我们使用 helper 就是这个
@await Component.InvokeAsync("Here is the name of your view component")
"component.InvokeAsynk" 进入你的组件类,找到Invoke方法然后按照这个方法
最佳答案
ViewComponent
上的Invoke
和InvokeAsync
方法是定义 View 组件逻辑的方法。这些不是匿名函数。它们是常规的 C# 方法,仅具有名称 Invoke
和 InvokeAsync
。
您不需要在 View 组件中同时定义它们。 相反,您只能定义一种方法。
假设您将其命名为 Invoke
然后将其重命名为 func
然后您更改了方法的名称:
- 你不会得到一个编译器错误,但是
- 将发生运行时错误。
这是因为调用您的 View 组件的 Asp.Net Core 框架代码期望它具有 Invoke
或 InvokeAsync
方法和您的 ViewComponent
现在两者都没有。
您可以随意命名您的 View 组件,但调用它的方法的名称必须称为 Invoke
或 InvokeAsync
。
一个 ViewComponent
只能有一个 Invoke
或 InvokeAsync
方法。 ViewComponent
不能同时拥有两者,也不能是其中任何一个的倍数(具有不同的参数)。尝试这样做会产生以下运行时错误:
View component 'ViewComponents.NameOfComponent' must have exactly one public method named 'InvokeAsync' or 'Invoke'.
关于c# - 什么是 Invoke 和 InvokeAsync 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43541027/