Working with static files

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

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 to the Startup class to be able to serve static files:

    app.UseStaticFiles();
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:

  1. Add DirectoryBrowsingMiddleware to the ConfigureService method of the Startup class right after calling the AddControllersWithViews() method:
        services.AddDirectoryBrowser(); 
  1. From within the Configure method of the Startup class, call the UseDirectoryBrowser method (after calling the UseCommunicationMiddleware method) to activate directory browsing:
        app.UseDirectoryBrowser(); 

The preceding code allows us to view the following root folders from the browser:

  1. Remove the call to the UseDirectoryBrowser method from the Startup class; we do not need it for the sample application.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.47.169