I understand that the order of registration for middleware may matter. However, it's not given that it's necessarily the case.
I noticed that UseDefaultFiles() needs to precede UseStaticFiles() (which can neatly be circumvented by UseFileServer()).
What I don't understand is why. How do they collide?!
I've googled the issue but got zero motivation on why the order is significant in this particular case. Only that it is of importance...
UseDefaultFiles is a URL rewriter that doesn't serve the file. With UseDefaultFiles , requests to a folder in wwwroot search for: default. htm. default.
By default, the wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root.
UseSpaStaticFiles(IApplicationBuilder) Configures the application to serve static files for a Single Page Application (SPA). The files will be located using the registered ISpaStaticFileProvider service.
ASP.NET Core application cannot serve static files by default. We must include Microsoft. AspNetCore. StaticFiles middleware in the request pipeline.
Taken from the documentation on Static files in ASP.NET Core (under Serve a default document, there's an important note).
UseDefaultFiles
must be called beforeUseStaticFiles
to serve the default file.UseDefaultFiles
is a URL rewriter that doesn't actually serve the file. Enable Static File Middleware viaUseStaticFiles
to serve the file.
Based on this, it's important to first setup the URL rewriter (UseDefaultFiles
) before serving the actual file (UseStaticFiles
).
If you don't, the UseStaticFiles
middleware will kick in first, but a request to the root of the application won't tell the middleware which 'file' to serve. When you make sure the rewrite is in place first, a request to the root of the application will have been rewritten to be a request for (one of the) default file(s).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With