I am reading trough the official tutorial from microsoft on creating an web api. https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1
Now I am trying it out in visual studio and I am doing it excactly as the tutorial describes it.
But I get 2 Error: 1) The Type or Namespacename "ApiController" is not found [CS0246] 2) Type "ActionResult" is not generic and can not be used with typearguments [CS0308]
Is the tutorial out of date or why am I getting these errors?
Here is the Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TodoApi.Models;
namespace TodoApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TodoController : ControllerBase
{
private TodoContext _context;
[HttpGet]
public ActionResult<List<TodoItem>> GetAll()
{
return _context.TodoItems.ToList();
}
[HttpGet("{id}", Name = "GetTodo")]
public ActionResult<TodoItem> GetById(long id)
{
var item = _context.TodoItems.Find(id);
if (item == null)
{
return NotFound();
}
return item;
}
public TodoController(TodoContext context)
{
_context = context;
if(_context.TodoItems.Count() == 0)
{
_context.TodoItems.Add(new TodoItem { Name = "Item1" });
_context.SaveChanges();
}
}
}
}
