Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to serve static files from outside the wwwroot folder?

I have an ASP.NET MVC 6 project with the following structure:

project/
  wwwroot/
  custom/
  project.json

I want to serve files from custom as it if was a virtual folder into http://localhost/custom without having to copy them during development.

Is it possible to do this in vNext without a virtual folder from IIS (say, using the StaticFile middleware)?

like image 327
Natan Avatar asked Aug 11 '15 16:08

Natan


People also ask

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.

How do you serve a static file?

To serve static files for Go 1.12+ in the standard environment, you define the handlers in your app. yaml file using either the static_dir or static_files elements. The content in the static files or static directories are unaffected by the scaling settings in your app.

How do I serve a static file in NET Core?

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

What file types should be placed in the wwwroot folder?

The wwwroot folder is new in ASP.NET 5 to store all of the static files in your project. Any files including HTML files, CSS files, image files, and JavaScript files which are sent to the user's browser should be stored inside this folder.


1 Answers

You can set the file provider on the options object when using the middleware.

app.UseStaticFiles(new StaticFileOptions() {
    FileProvider = new PhysicalFileProvider(@"C:\Path\To\Files"),
    RequestPath = new PathString("/somepath")
})

See: https://github.com/aspnet/StaticFiles/blob/master/src/Microsoft.AspNetCore.StaticFiles/Infrastructure/SharedOptions.cs#L44

and

https://github.com/aspnet/FileSystem/blob/dev/src/Microsoft.Extensions.FileProviders.Physical/PhysicalFileProvider.cs

like image 194
David Driscoll Avatar answered Oct 06 '22 21:10

David Driscoll