ASP.NETCore2.0Web API错误

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

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

现在我在 visual studio 中尝试它,我正在做它,正如教程所描述的那样。

但我得到 2 错误:1)找不到类型或命名空间“ApiController”[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

您必须安装.Net 核心 2.1 SDK,以便在创建项目时具有 2.1 版本选项。

像这样

enter image description here

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(477)
在程序文件与 Appdata中安装
上一篇
如何解决“指定的服务已被标记为删除”错误(the specified service is marked for deletio
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(39条)