Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you use multiple directories for static files in an aspnet core app?

Tags:

asp.net-core

By default, the wwwroot directory can host static files. However, in some scenarios, it might be ideal to have two directories for static files. (For example, having webpack dump a build into one gitignored directory, and keeping some image files, favicon and such in a not-gitignored directory). Technically, this could be achieved by having two folders within the wwwroot, but it might organizationally preferable to have these folders at the root level. Is there a way to configure an aspnet core app to use two separate directories for static files?

like image 747
John Avatar asked Dec 07 '16 15:12

John


People also ask

Where do I put static files in NET Core?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root.

How do you serve static files in ASP.NET Core web API?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).

Which folder is used to place the static resources in ASP MVC Core choose the answer?

ASP.NET Core API. Static files are typically located in the web root (wwwroot) folder. By default, that is the only place where we can serve up files directly from the file system.

How can add wwwroot folder in ASP.NET Core API?

In order to add the wwwroot folder, right-click on the project and then select add => new folder option and then provide the folder name as wwwroot.


1 Answers

Just register UseStaticFiles twice:

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "static"))
});

Now files will be found from wwwroot and static folders.

like image 60
juunas Avatar answered Sep 17 '22 09:09

juunas