我正在构建一个asp.net核心Web api项目。我正在尝试通过以下代码添加搜索查询。但是,在“视图”中出现“名称视图在当前上下文中不存在”的错误。
//https://localhost:44354/api/comments?searchString=Agree
[HttpGet("{searchString}")]
public async Task<IActionResult> Index(string searchString)
{
var comments = from c in _context.Comments
select c;
if (!String.IsNullOrEmpty(searchString))
{
comments = comments.Where(s => s.Comments_content.Contains(searchString));
}
return View(await comments.ToListAsync());
}
这是一个API。您返回的是响应而不是视图。您应该返回此
return Ok(await comments.ToListAsync());
此外,如果这是一个Web项目,则需要Index
在Views文件夹中创建相应的视图。例如,如果您的控制器名称为Blog
。然后Index
视图应该在此路径中Views/Blog/Index.cshtml
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句