Why does order between UseStaticFiles and UseDefaultFiles matter?












2















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










share|improve this question



























    2















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










    share|improve this question

























      2












      2








      2








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










      share|improve this question














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







      c# .net-core asp.net-core-2.2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 15:07









      Konrad VilterstenKonrad Viltersten

      12.5k32134256




      12.5k32134256
























          3 Answers
          3






          active

          oldest

          votes


















          4














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






          share|improve this answer































            2














            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.






            share|improve this answer































              2














              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://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document






              share|improve this answer
























              • 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 1 at 0: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 1 at 9:09











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988848%2fwhy-does-order-between-usestaticfiles-and-usedefaultfiles-matter%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














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






              share|improve this answer




























                4














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






                share|improve this answer


























                  4












                  4








                  4







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






                  share|improve this answer













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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 15:28









                  rickvdboschrickvdbosch

                  4,05621526




                  4,05621526

























                      2














                      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.






                      share|improve this answer




























                        2














                        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.






                        share|improve this answer


























                          2












                          2








                          2







                          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.






                          share|improve this answer













                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 31 '18 at 15:26









                          DavidGDavidG

                          70.2k9111129




                          70.2k9111129























                              2














                              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://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document






                              share|improve this answer
























                              • 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 1 at 0: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 1 at 9:09
















                              2














                              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://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document






                              share|improve this answer
























                              • 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 1 at 0: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 1 at 9:09














                              2












                              2








                              2







                              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://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document






                              share|improve this answer













                              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://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 31 '18 at 15:26









                              Manoj ChoudhariManoj Choudhari

                              1,5341516




                              1,5341516













                              • 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 1 at 0: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 1 at 9:09



















                              • 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 1 at 0: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 1 at 9:09

















                              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 1 at 0:51





                              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 1 at 0: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 1 at 9:09





                              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 1 at 9:09


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988848%2fwhy-does-order-between-usestaticfiles-and-usedefaultfiles-matter%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas