AspNetCore.Docs/aspnetcore/blazor/fundamentals/static-files.md

131 lines
7.0 KiB
Markdown
Raw Normal View History

2021-08-09 03:43:46 +08:00
---
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
2022-11-09 00:43:50 +08:00
ms.date: 11/08/2022
2021-08-09 03:43:46 +08:00
uid: blazor/fundamentals/static-files
---
# ASP.NET Core Blazor static files
[!INCLUDE[](~/includes/not-latest-version.md)]
2023-04-04 23:06:06 +08:00
2021-11-05 19:44:38 +08:00
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 <xref:blazor/tooling#visual-studio-solution-file-sln>.
2021-11-05 19:44:38 +08:00
## Static File Middleware
*This section applies to Blazor Server apps and the **:::no-loc text="Server":::** app of a hosted Blazor WebAssembly solution.*
2021-11-05 19:44:38 +08:00
Configure Static File Middleware to serve static assets to clients by calling <xref:Microsoft.AspNetCore.Builder.StaticFileExtensions.UseStaticFiles%2A> in the app's request processing pipeline. For more information, see <xref:fundamentals/static-files>.
## 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 <xref:Microsoft.Extensions.Hosting.Environments.Development> environment. To enable static files for environments other than <xref:Microsoft.Extensions.Hosting.Environments.Development> during local development and testing (for example, <xref:Microsoft.Extensions.Hosting.Environments.Staging>), call <xref:Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.UseStaticWebAssets%2A> on the <xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder> in `Program.cs`.
> [!WARNING]
> Call <xref:Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.UseStaticWebAssets%2A> 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 <xref:Microsoft.Extensions.Hosting.Environments.Staging> environment by calling <xref:Microsoft.Extensions.Hosting.HostEnvironmentEnvExtensions.IsStaging%2A>.
```csharp
if (builder.Environment.IsStaging())
{
builder.WebHost.UseStaticWebAssets();
}
```
2021-11-05 19:44:38 +08:00
## 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 `<StaticWebAssetBasePath>` property specified in the project file (`.csproj`) sets the base path to a non-root path:
```xml
<PropertyGroup>
<StaticWebAssetBasePath>{PATH}</StaticWebAssetBasePath>
</PropertyGroup>
```
In the preceding example, the `{PATH}` placeholder is the path.
2022-05-27 19:29:45 +08:00
The `<StaticWebAssetBasePath>` 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 <xref:blazor/host-and-deploy/multiple-hosted-webassembly>. The property is also effective in standalone Blazor WebAssembly apps.
2021-11-05 19:44:38 +08:00
Without setting the `<StaticWebAssetBasePath>` 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/`
2021-11-05 19:44:38 +08:00
* 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 `<StaticWebAssetBasePath>` 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`.
2021-11-05 19:44:38 +08:00
In the **:::no-loc text="Client":::** app's project file (`.csproj`) or the standalone Blazor WebAssembly app's project file (`.csproj`):
2021-11-05 19:44:38 +08:00
```xml
<PropertyGroup>
<StaticWebAssetBasePath>app1</StaticWebAssetBasePath>
</PropertyGroup>
```
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/`
2021-11-05 19:44:38 +08:00
* 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
2021-08-09 03:43:46 +08:00
To create additional file mappings with a <xref:Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider> or configure other <xref:Microsoft.AspNetCore.Builder.StaticFileOptions>, 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.
2021-10-07 21:20:17 +08:00
* Configure options through [dependency injection (DI)](xref:blazor/fundamentals/dependency-injection) in `Program.cs` using <xref:Microsoft.AspNetCore.Builder.StaticFileOptions>:
2021-08-09 03:43:46 +08:00
```csharp
using Microsoft.AspNetCore.StaticFiles;
...
var provider = new FileExtensionContentTypeProvider();
provider.Mappings["{EXTENSION}"] = "{CONTENT TYPE}";
2021-10-07 21:20:17 +08:00
builder.Services.Configure<StaticFileOptions>(options =>
2021-08-09 03:43:46 +08:00
{
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")`.
2021-10-07 21:20:17 +08:00
* Use two calls to <xref:Microsoft.AspNetCore.Builder.StaticFileExtensions.UseStaticFiles%2A> in `Program.cs`:
2021-08-09 03:43:46 +08:00
* Configure the custom file provider in the first call with <xref:Microsoft.AspNetCore.Builder.StaticFileOptions>.
* 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 <xref:Microsoft.AspNetCore.Builder.MapWhenExtensions.MapWhen%2A> to execute a custom Static File Middleware:
```csharp
app.MapWhen(ctx => !ctx.Request.Path
2021-11-19 20:32:41 +08:00
.StartsWithSegments("/_framework/blazor.server.js"),
subApp => subApp.UseStaticFiles(new StaticFileOptions() { ... }));
2021-08-09 03:43:46 +08:00
```
2021-11-05 19:44:38 +08:00
## Additional resources
* [App base path](xref:blazor/host-and-deploy/index#app-base-path)
2022-05-27 19:29:45 +08:00
* <xref:blazor/host-and-deploy/multiple-hosted-webassembly>