在我使用 VS2015 的 ASP.NET Core RC2 应用程序中,我在 ViewComponent 类中使用了以下 Invoke() 方法,但智能感知无法识别 Invoke() 方法。该应用程序构建成功,但在运行时出现以下错误:
System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Html.IHtmlContent]
ViewComponent类中的Invoke()方法:
public IViewComponentResult Invoke(string filter)
{
var res = (from p in ctx.Product.ToList()
where p.ProductName.StartsWith(filter)
select p).ToList();
return View(res);
}
从生成上述错误的 View 调用 Invoke() 方法:
<div class="alert alert-success">@Component.Invoke("ProductList", "D")</div>
最佳答案
这是一个老问题,但我想为 ASP.NET Core 的 1.0 RTM 版本提供一个答案。
@await Component.InvokeAsync("ProductList", new { filter = "D" })
Component.Invoke()
已被移除。因此,即使 View 组件中的方法仍定义为 Invoke()
(而不是 InvokeAsync()
),这也是正确的方法——这可能是好吧,这取决于方法的作用。
作为替代方案,您还可以使用更强类型的版本...
@(await Component.InvokeAsync<ProductList>(new { filter = "D" }))
关于c# - 无法识别 ASP.NET Core ViewComponent 的 Invoke() 方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37979547/