英文:
MapControllers() does not find any endpoints
问题
我刚刚从.NET Core 3.1 迁移到.NET 6。
我的项目是一个 WebApi。
我的所有控制器都继承自 ControllerBase,并且它们都有一个 [ApiController] 特性,具有适当的 [Route] 特性,我的端点也是一样的。
在.NET 3.1 中一切都运行得很好。
现在,所有我的调用都以 404 错误结束。
我尝试测试新的语法,尽管我目前不想这样做,结果还是一样的。
以下是我用于这个测试的 program.cs 文件:
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();
我的 SDK 版本是最新的(即 6.0.406)。
Microsoft 包的版本是 6.0.14。
我做错了什么?
英文:
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?
答案1
得分: 1
我最终找到了罪魁祸首。
问题是由遗留代码中嵌入项目的这两个包引起的:
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Analyzers" Version="2.2.0" />
可能存在冲突,但没有错误,所有我的控制器端点都被简单地忽略了。
删除它们解决了一切。
英文:
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.
答案2
得分: 0
如果你遇到以下错误:找不到网页,网址为:https://localhost:7285/swagger
尝试以下代码:
var builder = WebApplication.CreateBuilder(args);
// 将服务添加到容器中
builder.Services.AddControllers();
builder.Services.AddAuthorization();
// 了解有关配置Swagger/OpenAPI的更多信息,请访问 https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// 配置HTTP请求管道
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
希望这对你有帮助。
英文:
If you meet the error like: No webpage was found for the web address: https://localhost:7285/swagger
Try the below code:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddAuthorization();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论