0

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

        }        
    }
}
3
  • 1
    I'd be more inclined to believe you are trying to use ASP.NET Core 2.1 classes in an ASP.NET Core 2.0 project Commented Jun 22, 2018 at 18:08
  • Oh this could be possible. How can I create a ASP.NET Core 2.1 Project instead of a 2.0? Commented Jun 22, 2018 at 18:10
  • learn.microsoft.com/en-us/aspnet/core/migration/… Commented Jun 22, 2018 at 18:13

1 Answer 1

1

You must install .Net core 2.1 SDK in order to have 2.1 version option when create project.

Like this

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.