将 asp.net core web api 部署到 aws elastic beanstalk 时出现错误 404

Posted

技术标签:

【中文标题】将 asp.net core web api 部署到 aws elastic beanstalk 时出现错误 404【英文标题】:Getting Error 404 Not Found when deployed asp.net core web api to aws elastic beanstalk 【发布时间】:2021-12-23 23:47:36 【问题描述】:

所以我在 .NET 5.0 上构建了一个 asp.net core web api 应用程序,它在我的机器上运行良好。今天我使用 aws 工具将它部署在弹性 beantalk AWS 上。 它在部署和填充时不会显示任何错误。它只是返回我找不到代码 404。 这是我的代码。

using Startup.cs
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using ShopApiNet5.Authentication;
using ShopApiNet5.Models;


namespace ShopApiNet5

    public class Startup
    
        public Startup(IConfiguration configuration)
        
            Configuration = configuration;
        

        public IConfiguration Configuration  get; 

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        

            services.AddControllers();

            services.AddDbContext<ModelContext>(opt => opt.UseInMemoryDatabase("ModelDB"));

            services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ModelContext>()
                .AddDefaultTokenProviders();

            services.AddAuthentication(options =>
            
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            )

            .AddJwtBearer(options =>
            
                options.SaveToken = true;
                options.RequireHttpsMetadata = false;
                options.TokenValidationParameters = new TokenValidationParameters()
                
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidAudience = Configuration["JWT:ValidAudience"],
                    ValidIssuer = Configuration["JWT:ValidIssuer"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Secret"]))
                ;
            );

            services.AddSwaggerGen(c =>
            
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo  Title = "ShopApi", Version = "V1" );

                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                
                    Name = "Authorization",
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer",
                    BearerFormat = "JWT",
                    In = ParameterLocation.Header,
                    Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
                );
                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                
                    
                          new OpenApiSecurityScheme
                            
                                Reference = new OpenApiReference
                                
                                    Type = ReferenceType.SecurityScheme,
                                    Id = "Bearer"
                                
                            ,
                            new string[] 

                    
                );
            );
        

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        
            if (env.IsDevelopment())
            
                app.UseDeveloperExceptionPage();

                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TodoApi v1"));
            

            //app.UseHttpsRedirection();

            app.UseRouting();

app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            
                endpoints.MapControllers();
            );
        
    

这是我的 launchsettings.json


  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": 
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "launchUrl": "swagger",
    "iisExpress": 
      "applicationUrl": "http://shopapinetsm.us-east-1.elasticbeanstalk.com/",
      "sslPort": 44352
    
  ,
  "profiles": 
    "IIS Express": 
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": 
        "ASPNETCORE_ENVIRONMENT": "Development"
      
    ,
    "ShopApiNet5": 
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "launchUrl": "swagger",
      //"applicationUrl": "https://localhost:5001;http://localhost:5000",
      "applicationUrl": "http://shopapinetsm.us-east-1.elasticbeanstalk.com/",
      "environmentVariables": 
        "ASPNETCORE_ENVIRONMENT": "Production"
      
    
  

这里是查看最新日志的链接

https://elasticbeanstalk-us-east-1-676277819872.s3.amazonaws.com/resources/environments/logs/tail/e-afp9eu4yxa/i-00e8df513fd738529/TailLogs-1636655753712.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20211111T183555Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86399&X-Amz-Credential=AKIAIOUOORMVUTXOJUHQ%2F20211111%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=af742a78702d5d2e8131fe07d96e15b83449ffcbdacb0912a15acec32f371e3e

请帮帮我????

【问题讨论】:

我现在正在尝试使用标准的 asp.net 核心 web 应用程序模板来重新制作这个项目,而不是 web api。希望这对我有帮助 没有帮助,遇到同样的问题...shopapismnet5webapp-dev.us-east-1.elasticbeanstalk.com 链接到我重新制作的应用程序,在一个简单的网络应用程序模板上 我尝试部署默认模板 web api 应用程序,但未启用 https。它已部署但环境没有响应,我很伤心 我点击了你的日志,出现了Request has expired的错误信息。我查了资料。为了解决这个问题,对象的所有者必须生成一个新的具有新到期日期的预签名 URL。如果您是对象的所有者,请参阅预签名或与他人共享对象的说明。 谢谢@Chaodeng,其实我。解决了这个问题:) 【参考方案1】:

所以我刚刚做了一个没有启用 https 的新应用程序,它就像我记得的那样工作。

【讨论】:

以上是关于将 asp.net core web api 部署到 aws elastic beanstalk 时出现错误 404的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ASP Net Core Web API 上正确地将列表转换为 IQueryable

将文件从 ASP.NET Core Web api 发布到另一个 ASP.NET Core Web api

asp.net core 2 Web API 超时问题

将 ASP.NET Core 部署到 IIS UI 和 API 到单独的网站

Asp.Net Core Web API 应用程序:如何更改监听地址?

我正在将我的 asp.net web api 迁移到 asp.net core。 Cors 迁移