Hosting multiple SPA in a single ASP.NET core project

We would like to have several SPA, each accommodated with a different controller. Assme we publish the SPA to the root folder: wwwroot, e.g. wwwroot\app1, wwwroot\app2

Add the spa static files service:


builder.Services.AddSpaStaticFiles((options) =>
{
    options.RootPath = "wwwroot";
});

...

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
});

app.Map("/app1",
   _ =>
   {
       app.UseSpa(spa =>
       {
           spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions()
           {
               FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/app1"))
           };
       });
   });

app.Map("/app2",
  _ =>
  {
      superAdminApp.UseSpa(spa =>
      {
          spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions()
          {
              FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/app2"))
          };
      });

  });

Ensure that no controller has a route that intersects with the client applications route

Post a Comment

Previous Post Next Post