ASP.NET核心web api“;“当前上下文中不存在名称视图”;

ASP.NET核心web api“;“当前上下文中不存在名称视图”;,asp.net,rest,asp.net-core,asp.net-web-api,Asp.net,Rest,Asp.net Core,Asp.net Web Api,我正在构建一个asp.net核心web api项目。但是,我在“视图”中得到一个错误,“名称视图在当前上下文中不存在” //https://localhost:44354/api/comments?searchString=Agree [HttpGet(“{searchString}”)] 公共异步任务索引(字符串搜索字符串) { var comments=来自c中的_context.comments 选择c; 如果(!String.IsNullOrEmpty(searchString)) {

我正在构建一个asp.net核心web api项目。但是,我在“视图”中得到一个错误,“名称视图在当前上下文中不存在”

//https://localhost:44354/api/comments?searchString=Agree
[HttpGet(“{searchString}”)]
公共异步任务索引(字符串搜索字符串)
{
var comments=来自c中的_context.comments
选择c;
如果(!String.IsNullOrEmpty(searchString))
{
comments=comments.Where(s=>s.comments_content.Contains(searchString));
}
返回视图(wait comments.ToListAsync());
} 

这是一个api。您返回的是响应,而不是视图。你应该退回这个

return Ok(await comments.ToListAsync());
此外,如果这是一个web项目,则需要在视图文件夹中创建相应的视图,
Index
。例如,如果控制器名称为
Blog
。然后
索引
视图应位于此路径
视图/Blog/Index.cshtml

return Ok(await comments.ToListAsync());