繁体   English   中英

.Net Core 3 Web API 中的动作路由

[英]Action routing in .Net Core 3 Web API

我正忙于将现有工作的 WebApi 从 .Net Core 2.2 迁移到 3,但是,路由停止工作。 我不断收到 404 Not Found 消息。

想在我的控制器中使用动作名称作为路由模板的一部分,例如:

[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方法中,或者只使用新路由

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM