skip to Main Content

I have a project that requires retrieving files from outside itself on the server its hosted on. For now, I’m just grabbing a Javascript file but it will eventually grab other things. I tried out changing the content root path in Program.cs to allow this and it worked beautifully. But I noticed that the original relative links to Javascript and css files still worked without any changes.

For example this path, "~/css/site.css", still works despite changing the content root path and not telling it to specifically look in my project’s own wwwroot folder. The external file must use a more specific path. For example: "~/some-other-project/wwwroot/js/myscripts.js".

Now this is all fine and actually preferred behavior but I want to know why it’s working this way. Does the project check its own wwwroot folder first before looking elsewhere in the folder specified in the content root path? I want to understand the behavior behind this so I can feel more confident in using this method.

EDIT:
Here is a repo with as an example: https://github.com/love-bird-13/Custom-Content-Path-Test
The first commit is just setting up two basic .Net 6 projects without any changes. The second commit contains all the changes I made to reproduce what I’m talking about.

2

Answers


  1. This is explained in the docs in the web root section:

    The web root is the base path for public, static resource files, such as:

    • Stylesheets (.css)
    • JavaScript (.js)
    • Images (.png, .jpg)

    By default, static files are served only from the web root directory and its sub-directories. The web root path defaults to {content root}/wwwroot.

    And

    In Razor .cshtml files, ~/ points to the web root. A path beginning with ~/ is referred to as a virtual path.

    Login or Signup to reply.
  2. About your question

    Does the project check its own wwwroot folder first before looking elsewhere in the folder specified in the content root path?

    The Serve files outside wwwroot by updating IWebHostEnvironment.WebRootPath topic specifies below.

    When IWebHostEnvironment.WebRootPath is set to a folder other than wwwroot

    • In the development environment, static assets found in both wwwroot and the updated IWebHostEnvironment.WebRootPath are served from wwwroot.
    • In any environment other than development, duplicate static assets are served from the updated IWebHostEnvironment.WebRootPath folder.

    So the behavior you describe is applicable while in development environment mode.

    You’ll notice that when changing the ASPNETCORE_ENVIRONMENT environment variable to something else than Development – which is the default while developing – the files in wwwroot won’t be served anymore.


    That same topic gives some approaches for below scenarios.

    1. In case you want static content to only be served from the customized path instead of the default wwwroot, then the easiest might just be to delete that wwwroot folder.

    2. In case you want to combine wwwroot with a custom folder, then you add an additional app.UseStaticFiles in Program.cs; here below Assets is a custom folder with some static files, but it can be any absolute path that is reachable.

      app.UseStaticFiles();
      
      app.UseStaticFiles(new StaticFileOptions
      {
          FileProvider = new PhysicalFileProvider(
              Path.Combine(builder.Environment.ContentRootPath, "Assets")
              // Or some other absolute path. 
          )
      });
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search