【问题标题】:ASP.NET core web api "the name view does not exist in current context"ASP.NET core web api“当前上下文中不存在名称视图”
【发布时间】:2021-03-05 04:19:31
【问题描述】:

我正在构建一个 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());
        } 

【问题讨论】:

    标签: asp.net rest asp.net-core asp.net-web-api


    【解决方案1】:

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

    return Ok(await comments.ToListAsync());
    

    此外,如果这是一个 Web 项目,您需要在您的 Views 文件夹中创建相应的视图 Index。例如,如果您的控制器名称是 Blog。那么Index视图应该在这个路径Views/Blog/Index.cshtml

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多