简体   繁体   中英

deploy Angular/Asp.Net Core 2.1 app to AWS: 500 error

I can host my application locally and it works fine. I can publish to Elastic Beanstalk via AWS's VS wizard without issues. But when I try to access the application, I get a 500 error and nothing loads. Everything I've found references moving files from ClientApp/dist to wwwroot, but two things...

    1. I don't have a dist folder?
    2. the last line in `public IConfiguration Configuration` sets the root path for spa static files as dist

(forgive me, this is my first angular app deployment.)

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");
            }
        });
    }
}

I was able to circumvent this issue by creating a new project via the command line instead of using "File > New Project" in Visual Studio.

I created the new project with the command dotnet new angular

Then I copied all of the code from my old project to the new one. The new project published fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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