How to configure ASP.net Core server routing for multiple SPAs hosted with SpaServices

后端 未结 1 1741
鱼传尺愫
鱼传尺愫 2020-11-29 01:40

I have an Angular 5 application that I want to host with Angular Universal on ASP.net Core using the latest Angular template RC. I\'ve followed the docs and have the applica

1条回答
  •  感情败类
    2020-11-29 02:08

    Thanks to SteveSandersonMS and chris5287 over on Github for pointing me towards the solution on this.

    IApplicationBuilder.Map can segregate paths into different areas of concern. If you wrap a call to app.UseSpa in a call to app.Map, the SPA will be handled only for the path specified by the Map call. The app.UseSpa call ends up looking like:

    app.Map("/app1", app1 =>
    {
        app1.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501
    
            spa.Options.SourcePath = "ClientApp";
    
            spa.UseSpaPrerendering(options =>
            {
                options.BootModulePath = $"{spa.Options.SourcePath}/dist-server/main.bundle.js";
                options.BootModuleBuilder = env.IsDevelopment()
                    ? new AngularCliBuilder(npmScript: "build:ssr:en")
                    : null;
                options.ExcludeUrls = new[] { "/sockjs-node" };
                options.SupplyData = (context, data) =>
                {
                    data["foo"] = "bar";
                };
            });
    
            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start --app=app1 --base-href=/app1/ --serve-path=/");
            }
        });
    });
    

    You can make as many calls to app.Map as necessary to configure your SPAs. Also note the modification to the spa.UseAngularCliServer call at the end: you will need to set --base-href and --serve-path to match your particular config.

    0 讨论(0)
提交回复
热议问题