【问题标题】:deploy Angular/Asp.Net Core 2.1 app to AWS: 500 error将 Angular/Asp.Net Core 2.1 应用程序部署到 AWS:500 错误
【发布时间】:2019-01-08 01:24:14
【问题描述】:

我可以在本地托管我的应用程序,它工作正常。我可以毫无问题地通过 AWS 的 VS 向导发布到 Elastic Beanstalk。但是当我尝试访问应用程序时,我收到 500 错误并且没有加载任何内容。我发现的所有内容都引用了将文件从 ClientApp/dist 移动到 wwwroot,但有两件事......

    1.我没有 dist 文件夹?
    2. `public IConfiguration Configuration` 的最后一行将 spa 静态文件的根路径设置为 dist

(原谅我,这是我的第一个 Angular 应用部署。)

Startup.cs

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.AddDbContext<DataContext>(x => x
            .UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b =>
                b.MigrationsAssembly(("MyApp.App")))
            .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.IncludeIgnoredWarning)));

        services.AddMvc()
            .AddJsonOptions(opt =>
            {
                opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; });

        services.AddTransient<Seed>();

        services.AddCors();

        services.AddAutoMapper();

        services.AddScoped<IAuthRepository, AuthRepository>();

        services.AddScoped<IBaseRepository, BaseRepository>();

        var key = Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value);

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });

        services.AddScoped<LogUserActivity>();

        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseAuthentication();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler(builder =>
            {
                builder.Run(async context =>
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                    var error = context.Features.Get<IExceptionHandlerFeature>();
                    if (error != null)
                    {
                        context.Response.AddApplicationError(error.Error.Message);
                        await context.Response.WriteAsync(error.Error.Message);
                    }
                });
            });
        }

        app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
        app.UseHttpsRedirection();
        app.ConfigureSwagger(Assembly.GetExecutingAssembly());

        app.UseDefaultFiles();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
            }
        });
    }
}

【问题讨论】:

  • 500 表示未处理的异常。尝试注释掉 UseExceptionHandler 并将呼叫留给 UseDeveloperExceptionPage 以便您可以看到实际的异常消息。
  • 我仍然在浏览器控制台中收到 500 错误。浏览器窗口显示“启动应用程序时发生错误。”
  • 我建议检查日志以获取更多信息。我从未使用过豆茎,但我认为它在这里解释:docs.aws.amazon.com/elasticbeanstalk/latest/dg/…
  • @ChristophLütjen,查看日志文件后,我没有看到任何突出的内容。它表明部署和构建成功。但是,它没有提及我对服务器的请求。 (我希望它至少会注​​意到我提出了一个请求。)
  • dist 目录是作为 Angular 构建过程的一部分创建的。您会在 ASP.NET Core 2.1 Angular 模板生成的 &lt;projectRoot&gt;/ClientApp/.angular-cli.json 文件中找到它。我知道您提到它在本地托管时可以工作,但请确保使用dotnet publish 测试部署到一个目录,然后从该目录使用dotnet &lt;dllName&gt;。您可能会获得更多信息。

标签: c# angular amazon-web-services asp.net-core


【解决方案1】:

我能够通过命令行创建一个新项目来绕过这个问题,而不是在 Visual Studio 中使用“文件 > 新项目”。

我使用命令dotnet new angular创建了新项目

然后我将旧项目中的所有代码复制到新项目中。新项目发布正常。

【讨论】:

    猜你喜欢
    • 2021-05-08
    • 1970-01-01
    • 2021-04-08
    • 2019-01-21
    • 2020-12-24
    • 1970-01-01
    • 2020-10-29
    • 2019-02-02
    相关资源
    最近更新 更多