When working with web applications, most of the time, you have to work with HTML, CSS, JavaScript, and images, which are considered static files by ASP.NET Core 2.0.
Access to these files is not available by default, but you saw what needs to be done to allow static files to be used within your applications at the beginning of the chapter. In fact, you must add and configure the corresponding middleware in the Startup
class to be able to serve static files:
app.UseStaticFiles();
Note
Note that by default all static files served by this middleware are public and anyone can access them. If you need to protect some of your files, you need to either store them outside the wwwroot
folder or you need to use the FileResult
controller action, which supports the authorization middleware.
Furthermore, directory browsing is disabled by default for security reasons. You can, however, activate it easily if you need to allow users to see folders and files:
- Add the
DirectoryBrowsingMiddleware...