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

问题描述 投票:0回答:1

我正忙于将现有的工作正常的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)
   {

   }
}

有关此问题的任何建议?

c# asp.net-core-webapi asp.net-core-3.0
1个回答
0
投票

根据https://github.com/aspnet/AspNetCore/issues/8998,在.NET Core 3.0中,默认情况下,“动作名称”中将跳过Async。您的端点位于/api/Lookup/GetBranches。您可以通过以下方式更改此行为:

services.AddControllers();

with

services.AddControllers(options => options.SuppressAsyncSuffixInActionNames = false);

ConfigureServices方法中,或仅使用新路线

© www.soinside.com 2019 - 2024. All rights reserved.