--- title: ASP.NET Core Blazor static files author: guardrex description: Learn how to configure and manage static files for Blazor apps. monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc ms.date: 11/08/2022 uid: blazor/fundamentals/static-files --- # ASP.NET Core Blazor static files This article describes the configuration for serving static files in Blazor apps. For more information on *solutions* in sections that apply to hosted Blazor WebAssembly apps, see . :::moniker range=">= aspnetcore-6.0" ## Static File Middleware *This section applies to Blazor Server apps and the **:::no-loc text="Server":::** app of a hosted Blazor WebAssembly solution.* Configure Static File Middleware to serve static assets to clients by calling in the app's request processing pipeline. For more information, see . ## Static files in non-`Development` environments for Blazor Server apps *This section applies to Blazor Server apps.* In Blazor Server apps run locally, static web assets are only enabled by default in the environment. To enable static files for environments other than during local development and testing (for example, ), call on the in `Program.cs`. > [!WARNING] > Call for the ***exact environment*** to prevent activating the feature in production, as it serves files from separate locations on disk *other than from the project* if called in a production environment. The example in this section checks for the environment by calling . ```csharp if (builder.Environment.IsStaging()) { builder.WebHost.UseStaticWebAssets(); } ``` ## Static web asset base path *This section applies to standalone Blazor WebAssembly apps and hosted Blazor WebAssembly solutions.* By default, publishing a Blazor WebAssembly app places the app's static assets, including Blazor framework files (`_framework` folder assets), at the root path (`/`) in published output. The `` property specified in the project file (`.csproj`) sets the base path to a non-root path: ```xml {PATH} ``` In the preceding example, the `{PATH}` placeholder is the path. The `` property is most commonly used to control the paths to published static assets of multiple Blazor WebAssembly apps in a single hosted deployment. For more information, see . The property is also effective in standalone Blazor WebAssembly apps. Without setting the `` property, the client app of a hosted solution or a standalone app is published at the following paths: * In the **:::no-loc text="Server":::** project of a hosted Blazor WebAssembly solution: `/BlazorHostedSample/Server/bin/Release/{TFM}/publish/wwwroot/` * In a standalone Blazor WebAssembly app: `/BlazorStandaloneSample/bin/Release/{TFM}/publish/wwwroot/` In the preceding examples, the `{TFM}` placeholder is the [Target Framework Moniker (TFM)](/dotnet/standard/frameworks) (for example, `net6.0`). If the `` property in the **:::no-loc text="Client":::** project of a hosted Blazor WebAssembly app or in a standalone Blazor WebAssembly app sets the published static asset path to `app1`, the root path to the app in published output is `/app1`. In the **:::no-loc text="Client":::** app's project file (`.csproj`) or the standalone Blazor WebAssembly app's project file (`.csproj`): ```xml app1 ``` In published output: * Path to the client app in the **:::no-loc text="Server":::** project of a hosted Blazor WebAssembly solution: `/BlazorHostedSample/Server/bin/Release/{TFM}/publish/wwwroot/app1/` * Path to a standalone Blazor WebAssembly app: `/BlazorStandaloneSample/bin/Release/{TFM}/publish/wwwroot/app1/` In the preceding examples, the `{TFM}` placeholder is the [Target Framework Moniker (TFM)](/dotnet/standard/frameworks) (for example, `net6.0`). ## Blazor Server file mappings and static file options To create additional file mappings with a or configure other , use **one** of the following approaches. In the following examples, the `{EXTENSION}` placeholder is the file extension, and the `{CONTENT TYPE}` placeholder is the content type. * Configure options through [dependency injection (DI)](xref:blazor/fundamentals/dependency-injection) in `Program.cs` using : ```csharp using Microsoft.AspNetCore.StaticFiles; ... var provider = new FileExtensionContentTypeProvider(); provider.Mappings["{EXTENSION}"] = "{CONTENT TYPE}"; builder.Services.Configure(options => { options.ContentTypeProvider = provider; }); ``` Because this approach configures the same file provider used to serve `blazor.server.js`, make sure that your custom configuration doesn't interfere with serving `blazor.server.js`. For example, don't remove the mapping for JavaScript files by configuring the provider with `provider.Mappings.Remove(".js")`. * Use two calls to in `Program.cs`: * Configure the custom file provider in the first call with . * The second middleware serves `blazor.server.js`, which uses the default static files configuration provided by the Blazor framework. ```csharp using Microsoft.AspNetCore.StaticFiles; ... var provider = new FileExtensionContentTypeProvider(); provider.Mappings["{EXTENSION}"] = "{CONTENT TYPE}"; app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider }); app.UseStaticFiles(); ``` * You can avoid interfering with serving `_framework/blazor.server.js` by using to execute a custom Static File Middleware: ```csharp app.MapWhen(ctx => !ctx.Request.Path .StartsWithSegments("/_framework/blazor.server.js"), subApp => subApp.UseStaticFiles(new StaticFileOptions() { ... })); ``` ## Additional resources * [App base path](xref:blazor/host-and-deploy/index#app-base-path) * :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" ## Static File Middleware *This section applies to Blazor Server apps and the **:::no-loc text="Server":::** app of a hosted Blazor WebAssembly solution.* Configure Static File Middleware to serve static assets to clients by calling in the app's request processing pipeline. For more information, see . ## Static files in non-`Development` environments for Blazor Server apps *This section applies to Blazor Server apps.* In Blazor Server apps run locally, static web assets are only enabled by default in the environment. To enable static files for environments other than during local development and testing (for example, ), call on the in `Program.cs`. > [!WARNING] > Call for the ***exact environment*** to prevent activating the feature in production, as it serves files from separate locations on disk *other than the project* if called in a production environment. The example in this section checks explicitly for the environment. ```csharp Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { if (webBuilder.GetSetting(WebHostDefaults.EnvironmentKey) == Environments.Staging) { webBuilder.UseStaticWebAssets(); } webBuilder.UseStartup(); }); ``` ## Static web asset base path *This section applies to standalone Blazor WebAssembly apps and hosted Blazor WebAssembly solutions.* By default, publishing a Blazor WebAssembly app places the app's static assets, including Blazor framework files (`_framework` folder assets), at the root path (`/`) in published output. The `` property specified in the project file (`.csproj`) sets the base path to a non-root path: ```xml {PATH} ``` In the preceding example, the `{PATH}` placeholder is the path. The `` property is most commonly used to control the paths to published static assets of multiple Blazor WebAssembly apps in a single hosted deployment. For more information, see . The property is also effective in standalone Blazor WebAssembly apps. Without setting the `` property, the client app of a hosted solution or a standalone app is published at the following paths: * In the **:::no-loc text="Server":::** project of a hosted Blazor WebAssembly solution: `/BlazorHostedSample/Server/bin/Release/{TFM}/publish/wwwroot/` * In a standalone Blazor WebAssembly app: `/BlazorStandaloneSample/bin/Release/{TFM}/publish/wwwroot/` In the preceding examples, the `{TFM}` placeholder is the [Target Framework Moniker (TFM)](/dotnet/standard/frameworks) (for example, `net6.0`). If the `` property in the **:::no-loc text="Client":::** project of a hosted Blazor WebAssembly app or in a standalone Blazor WebAssembly app sets the published static asset path to `app1`, the root path to the app in published output is `/app1`. In the **:::no-loc text="Client":::** app's project file (`.csproj`) or the standalone Blazor WebAssembly app's project file (`.csproj`): ```xml app1 ``` In published output: * Path to the client app in the **:::no-loc text="Server":::** project of a hosted Blazor WebAssembly solution: `/BlazorHostedSample/Server/bin/Release/{TFM}/publish/wwwroot/app1/` * Path to a standalone Blazor WebAssembly app: `/BlazorStandaloneSample/bin/Release/{TFM}/publish/wwwroot/app1/` In the preceding examples, the `{TFM}` placeholder is the [Target Framework Moniker (TFM)](/dotnet/standard/frameworks) (for example, `net6.0`). ## Blazor Server file mappings and static file options To create additional file mappings with a or configure other , use **one** of the following approaches. In the following examples, the `{EXTENSION}` placeholder is the file extension, and the `{CONTENT TYPE}` placeholder is the content type. * Configure options through [dependency injection (DI)](xref:blazor/fundamentals/dependency-injection) in `Startup.ConfigureServices` (`Startup.cs`) using : ```csharp using Microsoft.AspNetCore.StaticFiles; ... var provider = new FileExtensionContentTypeProvider(); provider.Mappings["{EXTENSION}"] = "{CONTENT TYPE}"; services.Configure(options => { options.ContentTypeProvider = provider; }); ``` Because this approach configures the same file provider used to serve `blazor.server.js`, make sure that your custom configuration doesn't interfere with serving `blazor.server.js`. For example, don't remove the mapping for JavaScript files by configuring the provider with `provider.Mappings.Remove(".js")`. * Use two calls to in `Startup.Configure` (`Startup.cs`): * Configure the custom file provider in the first call with . * The second middleware serves `blazor.server.js`, which uses the default static files configuration provided by the Blazor framework. ```csharp using Microsoft.AspNetCore.StaticFiles; ... var provider = new FileExtensionContentTypeProvider(); provider.Mappings["{EXTENSION}"] = "{CONTENT TYPE}"; app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider }); app.UseStaticFiles(); ``` * You can avoid interfering with serving `_framework/blazor.server.js` by using to execute a custom Static File Middleware: ```csharp app.MapWhen(ctx => !ctx.Request.Path .StartsWithSegments("/_framework/blazor.server.js"), subApp => subApp.UseStaticFiles(new StaticFileOptions() { ... })); ``` ## Additional resources * [App base path](xref:blazor/host-and-deploy/index#app-base-path) * :::moniker-end :::moniker range="< aspnetcore-5.0" ## Static File Middleware Configure Static File Middleware to serve static assets to clients by calling in the app's request processing pipeline. Multiple calls to is supported when static files exist at multiple locations: ```csharp app.UseStaticFiles(); app.UseStaticFiles("/static"); ``` For more information, see . ## Static web asset base path *This section applies to standalone Blazor WebAssembly apps and hosted Blazor WebAssembly solutions.* By default, publishing a Blazor WebAssembly app places the app's static assets, including Blazor framework files (`_framework` folder assets), at the root path (`/`) in published output. The `` property specified in the project file (`.csproj`) sets the base path to a non-root path: ```xml {PATH} ``` In the preceding example, the `{PATH}` placeholder is the path. The `` property is most commonly used to control the paths to published static assets of multiple Blazor WebAssembly apps in a single hosted deployment. For more information, see . The property is also effective in standalone Blazor WebAssembly apps. Without setting the `` property, the client app of a hosted solution or a standalone app is published at the following paths: * In the **:::no-loc text="Server":::** project of a hosted Blazor WebAssembly solution: `/BlazorHostedSample/Server/bin/Release/{TFM}/publish/wwwroot/` * In a standalone Blazor WebAssembly app: `/BlazorStandaloneSample/bin/Release/{TFM}/publish/wwwroot/` In the preceding examples, the `{TFM}` placeholder is the [Target Framework Moniker (TFM)](/dotnet/standard/frameworks) (for example, `net6.0`). If the `` property in the **:::no-loc text="Client":::** project of a hosted Blazor WebAssembly app or in a standalone Blazor WebAssembly app sets the published static asset path to `app1`, the root path to the app in published output is `/app1`. In the **:::no-loc text="Client":::** app's project file (`.csproj`) or the standalone Blazor WebAssembly app's project file (`.csproj`): ```xml app1 ``` In published output: * Path to the client app in the **:::no-loc text="Server":::** project of a hosted Blazor WebAssembly solution: `/BlazorHostedSample/Server/bin/Release/{TFM}/publish/wwwroot/app1/` * Path to a standalone Blazor WebAssembly app: `/BlazorStandaloneSample/bin/Release/{TFM}/publish/wwwroot/app1/` In the preceding examples, the `{TFM}` placeholder is the [Target Framework Moniker (TFM)](/dotnet/standard/frameworks) (for example, `net6.0`). ## Blazor Server file mappings and static file options To create additional file mappings with a or configure other , use **one** of the following approaches. In the following examples, the `{EXTENSION}` placeholder is the file extension, and the `{CONTENT TYPE}` placeholder is the content type. * Configure options through [dependency injection (DI)](xref:blazor/fundamentals/dependency-injection) in `Startup.ConfigureServices` (`Startup.cs`) using : ```csharp using Microsoft.AspNetCore.StaticFiles; ... var provider = new FileExtensionContentTypeProvider(); provider.Mappings["{EXTENSION}"] = "{CONTENT TYPE}"; services.Configure(options => { options.ContentTypeProvider = provider; }); ``` Because this approach configures the same file provider used to serve `blazor.server.js`, make sure that your custom configuration doesn't interfere with serving `blazor.server.js`. For example, don't remove the mapping for JavaScript files by configuring the provider with `provider.Mappings.Remove(".js")`. * Use two calls to in `Startup.Configure` (`Startup.cs`): * Configure the custom file provider in the first call with . * The second middleware serves `blazor.server.js`, which uses the default static files configuration provided by the Blazor framework. ```csharp using Microsoft.AspNetCore.StaticFiles; ... var provider = new FileExtensionContentTypeProvider(); provider.Mappings["{EXTENSION}"] = "{CONTENT TYPE}"; app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider }); app.UseStaticFiles(); ``` * You can avoid interfering with serving `_framework/blazor.server.js` by using to execute a custom Static File Middleware: ```csharp app.MapWhen(ctx => !ctx.Request.Path .StartsWithSegments("/_framework/blazor.server.js"), subApp => subApp.UseStaticFiles(new StaticFileOptions() { ... })); ``` ## Additional resources * [App base path](xref:blazor/host-and-deploy/index#app-base-path) * :::moniker-end