Host spa(s) from paths off MVC app #187

Closed
opened 2025-08-09 17:15:20 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @tbertenshaw on 2/6/2018

Is it possible to define the spa routes as paths off an existing mvc site?
I.e. I want all normal defined mvc routing to work as expected, however if the relative path off the root is <base href="/memberspa"> for example (defined in the index.html) then i want that to serve my spa. Currently it seems that if i hit a route that exists by a existing mvc controller then that it is served by the controller. But if i pass any route which doesn't exist it loads my spa. i.e. /thisdoesnotexist redirects to /memberspa. But /home (which is served by my home controller) correctly is rendered by the home/index method.

If this is possible then i would ideally want to have two separate spa's served from paths off the sites root. The other potentially being /admin .

I'm currently using the following in my startup.cs

   if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseSpaStaticFiles();
            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";
             
                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }
*Originally created by @tbertenshaw on 2/6/2018* Is it possible to define the spa routes as paths off an existing mvc site? I.e. I want all normal defined mvc routing to work as expected, however if the relative path off the root is `<base href="/memberspa">` for example (defined in the index.html) then i want that to serve my spa. Currently it seems that if i hit a route that exists by a existing mvc controller then that it is served by the controller. But if i pass any route which doesn't exist it loads my spa. i.e. `/thisdoesnotexist` redirects to `/memberspa`. But /home (which is served by my home controller) correctly is rendered by the home/index method. If this is possible then i would ideally want to have two separate spa's served from paths off the sites root. The other potentially being `/admin` . I'm currently using the following in my startup.cs ```csharp if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript: "start"); } }); } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#187
No description provided.