15

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

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

3 Answers3

18

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

rickvdbosch
  • 14,105
  • 2
  • 40
  • 53
  • 1
    Sorry. what is the URL rewriter? what is "don't actually serve file"? I don't understand. – Spring Jul 05 '21 at 15:10
7

From the docs:

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.

The order of middleware does matter, this is why, for example, UseStaticFiles has to come before UseMvc as the MVC engine will handle all of the requests. In this case, UseDefaultFiles is simply rewriting the URL and passing it on to the UseStaticFiles middleware to serve.

DavidG
  • 113,891
  • 12
  • 217
  • 223
3

UseDefaultFiles() should always be before UseStaticFiles().

This is because UseDefaultFiles rewrite the URLs. Use Static files only serves the URLs.

If serving of document happens earlier than URL rewrite, then you may not get default document served.

Refernece:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
  • I thought that *UseStaticFiles()* set the flag to *true* to enable non-dynamical files to be accessible. As such, I can't see the problem in allowing the static files to be obtainable and then, in case the URL isn't pointing out an actual files, try to add *index.html*, *default.html* etc. What do you think? – Konrad Viltersten Jan 01 '19 at 00:51
  • In my opinion, we will have to understand how asp.net serves the files. The file serving middleware just maps actual url to the site resources. Once resources to be served is decided , it doesn't matter of the url stays same or it is rewritten. The sole purpose of url is done by that time. Just consider that if url is changed after checking the resources it may map to, then somehow framework will again have to handover request to previous module to serve contents. This may cause either code duplication, or performance issues as resource mapping has to be done twice. – Manoj Choudhari Jan 01 '19 at 09:09