Search code examples
asp.net-coreasp.net-mvc-routingasp.net-core-6.0

MapControllers() does not find any endpoints


I just migrated from .net core 3.1 to .net 6. My project is an WebApi. All my controllers inherit from ControllerBase and they have an [ApiController] attribute with the appropriate [Route] attribute. The same for my endpoints.

Everything was working great in .net 3.1.

Now, all my calls ends with an 404 error. I tried to test the new syntax, even if I don't want to at the moment, and the result is the same. Here is my program.cs file for this test:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAuthorization();
builder.Services.AddControllers();
var app = builder.Build();

app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.UseStaticFiles();
app.Run();

My sdk version is the latest (aka 6.0.406). And the Microsoft packages version is 6.0.14.

What do I do wrong?


Solution

  • I finally found the culprits. The problem was caused by these two packages from legacy code that were embbed in the project :

    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Analyzers" Version="2.2.0" />
    

    There may be a conflict somewhere but there was no error, all my controllers endpoints where simply ignored. Deleting them solved everything.