.. _fundamentals-static-files: Working with Static Files ========================= By `Rick Anderson`_ Static files, such as HTML, CSS, image, and JavaScript, are assets that an ASP.NET Core app can serve directly to clients. `View or download sample code `__ .. contents:: Sections :local: :depth: 1 Serving static files -------------------- Static files are typically located in the ``web root`` (*/wwwroot*) folder. See Content root and Web root in :doc:`/intro` for more information. You generally set the content root to be the current directory so that your project's ``web root`` will be found while in development. .. literalinclude:: ../../common/samples/WebApplication1/src/WebApplication1/Program.cs :language: c# :lines: 12-22 :emphasize-lines: 5 :dedent: 8 Static files can be stored in any folder under the ``web root`` and accessed with a relative path to that root. For example, when you create a default Web application project using Visual Studio, there are several folders created within the *wwwroot* folder - *css*, *images*, and *js*. The URI to access an image in the *images* subfolder: - \http:///images/ - \http://localhost:9189/images/banner3.svg In order for static files to be served, you must configure the :doc:`middleware` to add static files to the pipeline. The static file middleware can be configured by adding a dependency on the *Microsoft.AspNetCore.StaticFiles* package to your project and then calling the :dn:method:`~Microsoft.AspNetCore.Builder.StaticFileExtensions.UseStaticFiles` extension method from ``Startup.Configure``: .. literalinclude:: static-files/sample/StartupStaticFiles.cs :language: c# :start-after: >Configure :end-before: Configure :end-before: /StaticFiles/test.png`` will serve the *test.png* file. Static file authorization ------------------------- The static file module provides **no** authorization checks. Any files served by it, including those under *wwwroot* are publicly available. To serve files based on authorization: - Store them outside of *wwwroot* and any directory accessible to the static file middleware **and** - Serve them through a controller action, returning a :dn:class:`~Microsoft.AspNetCore.Mvc.FileResult` where authorization is applied Enabling directory browsing --------------------------- Directory browsing allows the user of your web app to see a list of directories and files within a specified directory. Directory browsing is disabled by default for security reasons (see Considerations_). To enable directory browsing, call the :dn:method:`~Microsoft.AspNetCore.Builder.DirectoryBrowserExtensions.UseDirectoryBrowser` extension method from ``Startup.Configure``: .. literalinclude:: static-files/sample/StartupBrowse.cs :language: c# :start-after: >Configure :end-before: Services :end-before: /MyImages, with links to each file and folder: .. image:: static-files/_static/dir-browse.png See Considerations_ on the security risks when enabling browsing. Note the two ``app.UseStaticFiles`` calls. The first one is required to serve the CSS, images and JavaScript in the *wwwroot* folder, and the second call for directory browsing of the *wwwroot/images* folder using the URL \http:///MyImages: .. literalinclude:: static-files/sample/StartupBrowse.cs :language: c# :start-after: >Configure :end-before: Configure :end-before: Configure :end-before: Configure :end-before: Services :end-before: /StaticFiles/test.png StaticFiles/test.png \http:///StaticFiles MyStaticFiles/default.html ========================================== =================================== If no default named files are in the *MyStaticFiles* directory, \http:///StaticFiles returns the directory listing with clickable links: .. image:: static-files/_static/db2.PNG .. note:: ``UseDefaultFiles`` and ``UseDirectoryBrowser`` will take the url \http:///StaticFiles without the trailing slash and cause a client side redirect to \http:///StaticFiles/ (adding the trailing slash). Without the trailing slash relative URLs within the documents would be incorrect. FileExtensionContentTypeProvider ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The :dn:class:`~Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider` class contains a collection that maps file extensions to MIME content types. In the following sample, several file extensions are registered to known MIME types, the ".rtf" is replaced, and ".mp4" is removed. .. literalinclude:: static-files/sample/StartupFileExtensionContentTypeProvider.cs :language: c# :start-after: >Configure :end-before: `__. Non-standard content types -------------------------- The ASP.NET static file middleware understands almost 400 known file content types. If the user requests a file of an unknown file type, the static file middleware returns a HTTP 404 (Not found) response. If directory browsing is enabled, a link to the file will be displayed, but the URI will return an HTTP 404 error. The following code enables serving unknown types and will render the unknown file as an image. .. literalinclude:: static-files/sample/StartupServeUnknownFileTypes.cs :language: c# :start-after: >Configure :end-before: /wwwroot*, away from application views, configuration files, etc. - The URLs for content exposed with ``UseDirectoryBrowser`` and ``UseStaticFiles`` are subject to the case sensitivity and character restrictions of their underlying file system. For example, Windows is case insensitive, but Mac and Linux are not. - ASP.NET Core applications hosted in IIS use the ASP.NET Core Module to forward all requests to the application including requests for static files. The IIS static file handler is not used because it doesn't get a chance to handle requests before they are handled by the ASP.NET Core Module. - To remove the IIS static file handler (at the server or website level): - Navigate to the **Modules** feature - Select **StaticFileModule** in the list - Tap **Remove** in the **Actions** sidebar .. warning:: If the IIS static file handler is enabled **and** the ASP.NET Core Module (ANCM) is not correctly configured (for example if *web.config* was not deployed), static files will be served. - Code files (including c# and Razor) should be placed outside of the app project's ``web root`` (*wwwroot* by default). This creates a clean separation between your app's client side content and server side source code, which prevents server side code from being leaked. Additional Resources -------------------- - :doc:`middleware` - :doc:`/intro`