I'm new to this and following this .Net ASP.NET wep api tutorial but cant get past this error. When doing the "Test the GetTodoItems method" and running the Postman to get/set against the DB.
When I start debugging, Chrome launches and the following error is thrown:
System.InvalidOperationException: 'The following errors occurred with attribute routing information:
Error 1: For action: 'TodoApi.Controllers.TodoController.GetTodoItems (TodoApi)' Error: While processing template 'api/[TodoController]', a replacement value for the token 'TodoController' could not be found. Available tokens: 'action, controller'. To use a '[' or ']' as a literal string in a route or within a constraint, use '[[' or ']]' instead.
Error 2: For action: 'TodoApi.Controllers.TodoController.GetTodoItem (TodoApi)' Error: While processing template 'api/[TodoController]/{id}', a replacement value for the token 'TodoController' could not be found. Available tokens: 'action, controller'. To use a '[' or ']' as a literal string in a route or within a constraint, use '[[' or ']]' instead.'
This is my controller code straight from the tutorial:
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;
}
}
}
Here is the 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();
}
}
}
As the error message suggests, the problem relates to building the routes for your TodoController. The key part of the message is this:
a replacement value for the token 'TodoController' could not be found
[controller]
in the route template is a token which ASP.NET Core will automatically replace when adding the route to the route table.
For example, given your controller is called TodoController
, the your route should be api/[controller]
,
[Route("api/[controller]")]
public class TodoController : ControllerBase {
//...
}
then the final route will be api/Todo
.
As noted by the exception, using the literal [TodoController]
isn't a known ASP.NET Core routing token. Which will cause the error when the framework tries to generate the attribute routes.
See Token replacement in route templates for more information.
I got a similar error until changed
[Route("api/[message]")]
On
[Route("api/message")]
I have changed from [Route("api/[TodoItemsController]")] to [Route("api/TodoItemsController")]. It worked. Yay
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With