代码之家  ›  专栏  ›  技术社区  ›  BlockchainProgrammer

ASP.NET Core 2.0 Web API错误

  •  0
  • BlockchainProgrammer  · 技术社区  · 6 年前

    我正在阅读微软创建网页API的官方教程。 https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1

    现在,我在visual studio中尝试它,并且按照教程中描述的那样做得非常出色。

    但我有两个错误: 1)没有找到类型或名称空间“ApCONTRONTROLL”[CS0246] 2)“actionResult”类型不是泛型,不能与typeArguments一起使用[cs0308]

    教程过时了吗?为什么我会出现这些错误?

    这里是控制器:

    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();
                }
    
            }        
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   user4851087    6 年前

    为了在创建项目时具有2.1版本选项,必须安装.NET Core 2.1 sdk。

    这样地

    enter image description here