Working with Static Files
Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients.
Static files are stored within the project's web root directory. The default directory is <content_root>/wwwroot, but it can be changed via the UseWebRoot
method.
Serve files inside of web root
Static files are accessible via a path relative to the web root. For example, the Web Application project template contains several folders within the wwwroot folder:
wwwroot
css
images
js
Invoke the UseStaticFiles
method within Startup.Configure
:
Serve files outside of web root
Consider a directory hierarchy in which the static files to be served reside outside of the web root:
wwwroot
css
images
js
MyStaticFiles
images
Static file authorization
The Static File Middleware doesn't provide authorization checks. Any files served by it, including those under wwwroot, are publicly accessible. To serve files based on authorization:
Store them outside of wwwroot and any directory accessible to the Static File Middleware.
Serve them via an action method to which authorization is applied. Return a
FileResult
object.
Non-standard content types
Static File Middleware understands almost 400 known file content types. If the user requests a file with an unknown file type, Static File Middleware passes the request to the next middleware in the pipeline. If no middleware handles the request, a 404 Not Found response is returned.
The following code enables serving unknown types and renders the unknown file as an image:
Last updated