What is the difference between UseStaticFiles, UseSpaStaticFiles, and UseSpa in ASP.NET Core 2.1?

前端 未结 2 1881
我在风中等你
我在风中等你 2020-12-13 08:50

ASP.NET Core 2.1.1 offers several seemingly related extension methods for appBuilder:

  • UseStaticFiles from Microsoft.AspNetCore.StaticFiles
2条回答
  •  借酒劲吻你
    2020-12-13 09:25

    Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients. Some configuration is required to enable serving of these files.

    • UseStaticFiles - Serve files inside of web root (wwwroot folder)

    • UseSpaStaticFiles - Serve static file like image, css, js in asset folder of angular app

    • UseSpa - let asp.net core know which directory you want to run your angular app, what dist folder when running in production mode and which command to run angular app in dev mode

    Example

    services.AddSpaStaticFiles(configuration =>
    {
     configuration.RootPath = "ClientApp/dist";
    });
    
    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");
        }
    });
    

提交回复
热议问题