我正忙于将现有的WebApi从.Net Core 2.2迁移到3,但是路由停止工作。我不断收到 404 未找到消息。
希望将操作名称用作控制器中路由模板的一部分,例如:
[Route("/api/[controller]/[action]")]
调用示例:/api/Lookup/GetBranchesAsync
我真的很困惑为什么它停止工作。
请参阅下面的代码。
启动:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<IAuthService, AuthService>();
services.AddScoped<ILookupService, LookupService>();
services.AddScoped<IFranchiseRepo, FranchiseRepo>();
services.AddScoped<ILogRepo, LogRepo>();
services.AddSingleton<IConfiguration>(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
控制器:
[ApiController]
[Route("/api/[controller]/[action]")]
[Produces("application/json")]
public class LookupController : Controller
{
private readonly ILookupService lookupService;
public LookupController(ILookupService lookupService)
{
this.lookupService = lookupService;
}
[HttpGet]
public async Task<IActionResult> GetBranchesAsync()
{
}
[HttpGet("{branchID}")]
public async Task<IActionResult> GetBranchSEAsync(int? branchID)
{
}
}
关于问题可能是什么的任何建议?
根据https://github.com/aspnet/AspNetCore/issues/8998,在.NET Core 3.0中,默认情况下在操作名称中跳过Async
。您的端点可在/api/Lookup/GetBranches
.您可以通过替换来更改此行为
services.AddControllers();
跟
services.AddControllers(options => options.SuppressAsyncSuffixInActionNames = false);
ConfigureServices
方法,或仅使用新路由