简体   繁体   中英

How to serve SPA at host/app path with ASP.Net Core

I'm using the Angular dotnet template, which makes use of the Spa related extensions.

What changes do I need to make in order for the SPA to be served at localhost:5001/app instead of localhost:5001 ?

Here's what the Startup.cs file looks like:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace angular_dotnet
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            // if (!env.IsDevelopment())
            // {
                app.UseSpaStaticFiles();
            // }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
            });

            app.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";

                /* if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                } */
            });
        }
    }
}

To make this work, here's what I ended up adding to the bottom of my Startup.cs, after removing all other use of SPA related extensions throughout the file.

app.Map(new PathString("/app"), client =>
{       
    var clientPath = Path.Combine(Directory.GetCurrentDirectory(), "./ClientApp/dist");
    StaticFileOptions clientAppDist = new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(clientPath)
    };

         
    client.UseSpaStaticFiles(clientAppDist);
    
    client.UseSpa(spa =>
     {
         spa.Options.DefaultPageStaticFileOptions = clientAppDist;
     });                                       
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM