首页 新闻 会员 周边

ASP.NET Core view component 运行时报错:"A view component must return a non-null value"

0
悬赏园豆:30 [已解决问题] 解决于 2019-06-09 09:26

在一个 asp.net core 3.0 preview 5 中实现了一个 view component ,运行时出现下面的错误:

System.InvalidOperationException: A view component must return a non-null value.
   at Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentInvoker.CoerceToViewComponentResult(Object value)

InvokeAsync 方法的实现如下:

public async Task<IViewComponentResult> InvokeAsync(AggSiteModel model)
{
    if (model == null) return null;

    model.Posts ??= await GetPostsAsync(model);
    if (model.Posts == null) return null;

    return View("PostList", model.Posts);
}

请问如何解决?

dudu的主页 dudu | 高人七级 | 园豆:30948
提问于:2019-06-09 08:40
< >
分享
最佳答案
0

return null 引起的,改为 return Content(string.Empty) 就可以了

dudu | 高人七级 |园豆:30948 | 2019-06-09 09:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册