【问题标题】:Any Suggestions for getting ASP.NET Core + React Running on Elastic Beanstalk让 ASP.NET Core + React 在 Elastic Beanstalk 上运行的任何建议
【发布时间】:2021-02-01 15:57:27
【问题描述】:

我正在尝试将带有 ReactJS 应用程序的 ASP.NET Core 部署到 Amazon 的 Elastic Beanstalk。我一直在使用this tutorial 来帮助我入门。我可以很好地部署教程(使用dotnet new web 模板)项目。但是,当我发布 ASP.NET Core + React 项目(使用 dotnet new react 模板)时,尝试访问应用程序时出现以下异常:

InvalidOperationException: The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request.
Your application is running in Production mode, so make sure it has been published, or that you have built your SPA manually. Alternatively you may wish to switch to the Development environment.
  • 只有当我尝试访问ClientApp/ React 组件时才会出现这种情况。当我访问 API 端点时,没有问题。
  • 此外,在本地运行时不会发生这种情况。在本地运行正常。

为了重现此错误,我执行了以下操作:

dotnet new react -o test-react/
dotnet publish test-react/ -o site/
cd site/
zip ../deploy.zip *

最后,我手动将 deploy.zip 导入 AWS Elastic Beanstalk。

这是该项目的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.AddControllersWithViews();

        // In production, the React files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });
    }

    // 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();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // 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.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
        });

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

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });
    }
}

作为参考,我的目标是 .NET Core 3.1。关于如何解决这个问题的任何想法?我相信我已经尝试了this GitHub 问题上建议的所有内容。任何帮助将不胜感激。

这个问题看起来很相似,但显然是针对 Angular 而不是 React: deploy Angular/Asp.Net Core 2.1 app to AWS: 500 error

【问题讨论】:

    标签: reactjs amazon-web-services asp.net-core


    【解决方案1】:

    原来我的deploy.zip 包没有被递归创建,所以子目录中的文件丢失了。而不是这样做,

    zip ../deploy.zip *
    

    我做到了,

    zip -r ../deploy.zip *
    

    按预期工作。傻我。

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 2013-06-13
      • 1970-01-01
      • 2013-04-21
      • 2014-06-20
      • 2017-03-11
      • 2015-06-18
      • 2014-06-02
      • 2019-03-14
      相关资源
      最近更新 更多