Manage two SPA in a single ASP.NET Core 3.1 Web API; One in the root and the other in "admin" route

Viewed 370

I have this ASP.NET Core 3.1 Web API application which is used by 2 SPA Angular applications; one in "app" route and the other in "admin" route.

The routing piece of code is like below:

app.Map("/app", client => // =======> changed to "/" but no used
            {
                client.UseSpa(spa =>
                {
                spa.Options.SourcePath = "app";
                spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "app")),
                    
                };
                });
            }).Map("/admin", admin =>
            {
                admin.UseSpa(spa =>
                {
                    spa.Options.SourcePath = "admin";
                    spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
                    {
                        FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "admin")),
                      
                    };
                });
            });

I need to go to the first application (app) in the root. I tried to change the route "/app" to "/" but it did not work.

1 Answers

To serve the main app at root, remove its SPA middleware from inside the .Map() branch and move it after the admin app's middleware.

// ...
app.Map("/admin", adminApp => 
    adminApp.UseSpa(spa => {
        spa.Options.SourcePath = "admin";
        // ...
    })
);

app.UseSpa(spa => {
    spa.Options.SourcePath = "app";
    // ...
});
// ...

So when a request to /admin* comes, it will follow the branch created by .Map("/admin") and be served by the admin SPA middleware. For all other requests, the main branch will execute, and the main app will be served.

Related