ASP.NET Core Web API 在本地运行,但不在 Azure 应用服务上运行

Jia*_*Tan 2 asp.net azure asp.net-web-api asp.net-web-api-routing asp.net-core-2.1

我一直严格遵循此链接(https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio)来创建我的Web api 。

**在本地,它运行良好。测试了链接并返回 JSON 数据

但是,一旦我将 Web api 部署到 azure 应用程序服务,我的所有 api 链接都会返回错误 404。是否有任何我可能错过的路由?

在我的控制器中,我已将其添加到我的头脑中。

[Route("api/xxx")]
[ApiController]
Run Code Online (Sandbox Code Playgroud)

在每个函数中,我都添加了这个

[HttpPut("xxx/{Id}")]
Run Code Online (Sandbox Code Playgroud)

至于我的程序/启动它与教程完全相同

  1. 节目类

    public class Program
    {
       public static void Main(string[] args)
       {
            CreateWebHostBuilder(args).Build().Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 启动.cs

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
    
        app.UseHttpsRedirection();
        app.UseMvc();
    }
    
    Run Code Online (Sandbox Code Playgroud)

    如果您需要更多信息,请告诉我。非常感谢任何帮助,谢谢!

Ism*_*mar 7

我知道答案已经帮助您解决了您的问题。但我也遇到过类似的情况。就我而言,我的 api 已启动并正在运行,但当我导航到 https://api.mydomain.com 时,我期望看到的 Swagger 页面显示错误 404 not found,这让我认为该应用程序无法正常工作。

经过几个小时的调整和打开天蓝色应用程序服务调试以及日志流打开和关闭。我注意到我的应用程序中的hangfire 作业正在触发并正在运行。

所以我回去检查我的代码。

这几乎就是我所做的,让我认为应用程序服务无法正常工作。事实上效果很好。

启动.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, UserManager<AppUser> userManager,
           RoleManager<IdentityRole> roleManager)
      {
           //Stripped Down for readability purpose

           app.UseSwagger();
           if (!env.IsProduction())
           {
                app.UseSwaggerUI(c =>
                {
                     c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample API V1");
                     c.RoutePrefix = string.Empty;
                });
           }

           if (!env.IsProduction())
           {
                app.UseHangfireDashboard();
           }
           else
           {
                app.UseHangfireDashboard("/hangfire", new DashboardOptions
                {
                     Authorization = new[] { new HangfireAuthorizationFilter() }
                });
           }
           app.UseHangfireServer();
      }
Run Code Online (Sandbox Code Playgroud)

所以,就我而言,正是这一行导致应用程序服务返回错误 404

app.UseSwagger();
           if (!env.IsProduction())
           {
                app.UseSwaggerUI(c =>
                {
                     c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample API V1");
                     c.RoutePrefix = string.Empty;
                });
           }
Run Code Online (Sandbox Code Playgroud)

因为我只希望 Swagger 页面仅在测试或开发环境中显示,而不是在生产环境中显示。但我内心期望这个招摇的页面会出现并显示。

我希望这对任何处于我这种情况的人也有帮助。