c# - 处理模板 api/[TodoController] 时,找不到令牌 TodoController 的替换值
问题描述
我是新手并遵循这个.Net ASP.NET wep api 教程,但无法克服这个错误。在执行“测试 GetTodoItems 方法”并运行邮递员以获取/设置数据库时。
当我开始调试时,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;
}
}
}
这是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();
}
}
}
解决方案
正如错误消息所暗示的,问题与为 TodoController 构建路由有关。消息的关键部分是这样的:
找不到令牌“TodoController”的替换值
[controller]
路由模板中的标记是 ASP.NET Core 在将路由添加到路由表时会自动替换的标记。
例如,假设你的控制器被调用TodoController
,你的路线应该是api/[controller]
,
[Route("api/[controller]")]
public class TodoController : ControllerBase {
//...
}
那么最后的路线将是api/Todo
。
正如异常所指出的,使用文字[TodoController]
不是已知的 ASP.NET Core 路由令牌。当框架尝试生成属性路由时,这将导致错误。
有关更多信息,请参阅路由模板中的令牌替换。
推荐阅读
- c# - C#反序列化自动将字符串反序列化为字节数组以通过自定义代码进行属性?
- javascript - 基于 Three.js 中的新枢轴动画旋转对象
- jenkins - Jenkins 在 git repo 中找不到 sh 脚本
- java - 修复 Java db 与 sqlserver 连接中的“找不到适合 jdbc 的驱动程序”错误
- django - django中的example.com密码重置
- ios - 如何重现罕见的“_CFAutoReleasePoolPop”崩溃?
- javascript - 我不能让所有的 Javascript 计算一起工作
- php - 使用 postgres 和 php 将 oid 导出到文件
- c - 指向 volatile 结构成员的 const 指针
- excel - 如何从 Microsoft Excel 的大型相关下拉菜单中删除空白选项?