![](/img/trans.png)
[英]ASP.NET Web API 2.0 pipeline and ASP.NET Core Web API pipeline differences
[英]ASP.NET Core 2.0 Web Api Error
我正在閱讀有關創建Web API的Microsoft官方教程。 https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1
現在,我正在Visual Studio中對其進行嘗試,並且正如本教程中所描述的那樣,我正在精確地進行操作。
但是我得到2錯誤:1)找不到類型或名稱空間名稱“ ApiController” [CS0246] 2)類型“ ActionResult”不是通用的,不能與類型參數一起使用[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();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.