Quantcast
Channel: How to host multiple React SPA apps on a single ASP.NET Core site? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Itinerati for How to host multiple React SPA apps on a single ASP.NET Core site?

$
0
0

I've come up with a solution that's working for me, based on the answer by @cminus.

First, I created this helper function, where codeFolderPath is the where my app folder is, and spaPath is the directory where I want the application hosted:

private void EnableReactApp(IApplicationBuilder app, IWebHostEnvironment env, string codeFolderPath, string spaPath)        {            if (env.IsDevelopment())            {                app.MapWhen(y => y.Request.Path.StartsWithSegments(spaPath) ||                                 y.Request.Path.StartsWithSegments("/sockjs-node") ||                                 y.Request.Path.StartsWithSegments("/static"), client =>                {                    client.UseSpa(spa =>                    {                        spa.Options.SourcePath = codeFolderPath;                        spa.UseReactDevelopmentServer(npmScript: "start");                    });                });            }            else            {                app.Map(new PathString(spaPath), client =>                {                    client.UsePathBase(new PathString(spaPath));                    client.UseSpaStaticFiles();                    client.UseSpa(spa => {                       spa.Options.SourcePath = codeFolderPath;});                });            }        }

Then, I add the following lines to my Startup.cs:

        public void ConfigureServices(IServiceCollection services)        {            [...]            services.AddSpaStaticFiles(configuration => { configuration.RootPath = "SpaApp1/build"; });            services.AddSpaStaticFiles(configuration => { configuration.RootPath = "SpaApp2/build"; });            [...]        }        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)        {            [...]            EnableReactApp(app, env, "SpaApp1", "/admin");            EnableReactApp(app, env, "SpaApp2", "/sandbox");            [...]        }

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>