ASP.NET core web api “the name view does not exist in current context”

I am building an asp.net core web api project.I am trying to add a search query through the following code. However, I am getting an error at the “view” that “the name view does not exist in current context”.

//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());
        }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is an api. You are returning a response not a view. You should return this instead

return Ok(await comments.ToListAsync());

Besides, if this was a web project you need to have the corresponding view, Index created in your Views folder. For example if your controller name is Blog. Then the Index view should be in this path Views/Blog/Index.cshtml


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x