Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does order between UseStaticFiles and UseDefaultFiles matter?

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...

like image 626
Konrad Viltersten Avatar asked Dec 31 '18 15:12

Konrad Viltersten


People also ask

What is UseDefaultFiles?

UseDefaultFiles is a URL rewriter that doesn't serve the file. With UseDefaultFiles , requests to a folder in wwwroot search for: default. htm. default.

What is use of Wwwroot folder in ASP.NET Core?

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.

What is UseSpaStaticFiles?

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.

Can .NET Core HTTP pipeline be configured to server static files whose directory hierarchy reside outside of the web root?

ASP.NET Core application cannot serve static files by default. We must include Microsoft. AspNetCore. StaticFiles middleware in the request pipeline.


1 Answers

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 before UseStaticFiles to serve the default file. UseDefaultFiles is a URL rewriter that doesn't actually serve the file. Enable Static File Middleware via UseStaticFiles 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).

like image 151
rickvdbosch Avatar answered Sep 30 '22 20:09

rickvdbosch