1应用程序部署到AWS

1应用程序部署到AWS

本文介绍了将Angular/Asp.Net Core 2.1应用程序部署到AWS:500错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在本地托管我的应用程序,并且可以正常运行.我可以通过AWS的VS向导发布到Elastic Beanstalk,而不会出现问题.但是,当我尝试访问该应用程序时,出现500错误,并且没有任何负载.我发现的所有内容都涉及将文件从ClientApp/dist移至wwwroot,但有两件事...

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.我没有dist文件夹吗?
    2."public IConfiguration Configuration"的最后一行将spa静态文件的根路径设置为dist
    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.)

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

推荐答案

我能够通过在命令行中而不是在Visual Studio中使用文件>新建项目"来通过创建一个新项目来规避此问题.

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.

我使用命令 dotnet new angular

然后,我将所有代码从旧项目复制到新项目.新项目发布良好.

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

这篇关于将Angular/Asp.Net Core 2.1应用程序部署到AWS:500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:47