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

C#asp.net Coreasp.net Core-2.0asp.net Core-2.1

C# Problem Overview


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

  • UseStaticFiles from Microsoft.AspNetCore.StaticFiles
  • UseSpaStaticFiles from Microsoft.AspNetCore.SpaServices.Extensions
  • UseSpa from Microsoft.AspNetCore.SpaServices.Extensions

Please help me make sense of their purpose and relation to each other?

Also, is there any difference from the server execution standpoint if I run these methods in a different order

e.g.

app.UseStaticFiles() -> app.UseSpaStaticFiles() -> app.UseSpa()

vs

app.UseSpa() -> app.UseSpaStaticFiles() -> app.UseStaticFiles()

C# Solutions


Solution 1 - C#

> 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");
    }
});

Solution 2 - C#

  1. UseStaticFiles serves files from wwwroot but it can be changed.
  2. UseSpaStaticFiles does a similar thing but it requires ISpaStaticFileProvider to be registered. If app.ApplicationServices.GetService<ISpaStaticFileProvider>() returns null, then you will get an exception.
throw new InvalidOperationException($"To use {nameof(UseSpaStaticFiles)}, you must " +
                    $"first register an {nameof(ISpaStaticFileProvider)} in the service provider, typically " +
                    $"by calling services.{nameof(AddSpaStaticFiles)}.");

So you need to call app.AddSpaStaticFiles() to register default ISpaStaticFileProvider

  1. UseSpa does two things. Rewrites all requests to the default page and tries to configure static files serving. On the contrary to UseSpaStaticFiles it does not throw an exception but just falls back to wwwroot folder.

Actually UseSpaStaticFiles and UseSpa both internally call the same method UseSpaStaticFilesInternal but with a different value for the 3rd parameter which is allowFallbackOnServingWebRootFiles. That is the reason why UseSpaStaticFiles throws an exception if no ISpaStaticFileProvider was registered it simply does not allow to fall back to wwwroot.

BTW if UseSpa falls back to wwwroot internally it calls old good app.UseStaticFiles(staticFileOptions);

Links to github sources:

  1. SpaDefaultMiddleware
  2. SpaStaticFilesExtensions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIgor SoloydenkoView Question on Stackoverflow
Solution 1 - C#user4851087View Answer on Stackoverflow
Solution 2 - C#Oleksandr TaranView Answer on Stackoverflow