在处理模板 api/[TodoController] 时,找不到令牌 TodoController 的替换值

Exo*_*Kid 5 c# asp.net-core

我是新手,正在关注这个.Net ASP.NET wep api 教程,但无法克服这个错误。在执行“测试 GetTodoItems 方法”并运行 Postman 以获取/设置数据库时。

当我开始调试时,Chrome 启动并抛出以下错误:

System.InvalidOperationException: '属性路由信息发生以下错误:

错误 1:对于操作:'TodoApi.Controllers.TodoController.GetTodoItems (TodoApi)' 错误:在处理模板 'api/[TodoController]' 时,找不到令牌 'TodoController' 的替换值。可用令牌:“动作,控制器”。要将“[”或“]”用作路由或约束中的文字字符串,请改用“[[”或“]]”。

错误 2:对于操作:'TodoApi.Controllers.TodoController.GetTodoItem (TodoApi)' 错误:处理模板 'api/[TodoController]/{id}' 时,找不到令牌 'TodoController' 的替换值。可用令牌:“动作,控制器”。要将“[”或“]”用作路由或约束中的文字字符串,请改用“[[”或“]]”。

这是我直接从教程中获取的控制器代码:

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TodoApi.Models;

namespace TodoApi.Controllers
{
    [Route("api/[TodoController]")]
    [ApiController]
    public class TodoController : ControllerBase
    {
        private readonly TodoContext _context;

        public TodoController(TodoContext context)
        {
            _context = context;

            if (_context.TodoItems.Count() == 0)
            {
                // Create a new TodoItem if collection is empty,
                // which means you can't delete all TodoItems.
                _context.TodoItems.Add(new TodoItem { Name = "Item1" });
                _context.SaveChanges();
            }
        }

        // GET: api/Todo
        [HttpGet]
        public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
        {
            return await _context.TodoItems.ToListAsync();
        }

        // GET: api/Todo/5
        [HttpGet("{id}")]
        public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
        {
            var todoItem = await _context.TodoItems.FindAsync(id);

            if (todoItem == null)
            {
                return NotFound();
            }

            return todoItem;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TodoApi.Models;

namespace TodoApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the 
        //container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<TodoContext>(opt =>
                opt.UseInMemoryDatabase("TodoList"));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP 
        //request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for 
                // production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ica 19

正如错误消息所暗示的那样,问题与为 TodoController 构建路由有关。消息的关键部分是这样的:

找不到令牌“TodoController”的替换值

[controller] 在路由模板中是一个令牌,当将路由添加到路由表时,ASP.NET Core 将自动替换它。

例如,假设你的控制器被调用TodoController,你的路线应该是api/[controller]

[Route("api/[controller]")]
public class TodoController : ControllerBase {
    //...
}
Run Code Online (Sandbox Code Playgroud)

那么最终的路线将是api/Todo

正如异常所指出的,使用文字[TodoController]不是已知的 ASP.NET Core 路由标记。当框架尝试生成属性路由时,这将导致错误。

有关更多信息,请参阅路由模板中的令牌替换