You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When dealing with large collections of assets, which is considered to be around 1,000 or more assets, we recommend using a bundler to reduce the final number of assets served by the app or to combine <xref:Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets%2A> with <xref:Microsoft.AspNetCore.Builder.StaticFileExtensions.UseStaticFiles%2A>.
369
+
370
+
<xref:Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets%2A> eagerly loads the precomputed metadata captured during the build process for the resources in order to support compression, caching, and fingerprinting. These features come at the cost of greater memory usage by the app. For assets that are frequently accessed, it's usually worth the costs. For assets that aren't frequently accessed, the trade-off might not be worth the costs.
371
+
372
+
If you don't use bundling, we recommend that you combine <xref:Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets%2A> with <xref:Microsoft.AspNetCore.Builder.StaticFileExtensions.UseStaticFiles%2A>. The following example demonstrates the approach.
373
+
374
+
In the project file (`.csproj`), the `StaticWebAssetEndpointExclusionPattern` MSBuild property is used to filter endpoints from the final manifest for <xref:Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets%2A>. Excluded files are served by <xref:Microsoft.AspNetCore.Builder.StaticFileExtensions.UseStaticFiles%2A> and don't benefit from compression, caching, and fingerprinting.
375
+
376
+
When setting the value of `StaticWebAssetEndpointExclusionPattern`, retain `$(StaticWebAssetEndpointExclusionPattern)` to keep the framework's default exclusion pattern. Add additional patterns in a semicolon-separated list.
377
+
378
+
In the following example, the exclusion patten adds the static files in the `lib/icons` folder, which represents a hypothetical batch of icons:
After HTTPS Redirection Middleware (`app.UseHttpsRedirection();`) processing in the `Program` file:
387
+
388
+
* Call <xref:Microsoft.AspNetCore.Builder.StaticFileExtensions.UseStaticFiles%2A> to handle the excluded files (`lib/icons/**`) and any other files not covered by <xref:Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets%2A>.
389
+
* Call <xref:Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets%2A> after <xref:Microsoft.AspNetCore.Builder.StaticFileExtensions.UseStaticFiles%2A> to handle critical application files (CSS, JS, images).
390
+
391
+
```csharp
392
+
app.UseStaticFiles();
393
+
394
+
app.UseAuthorization();
395
+
396
+
app.MapStaticAssets();
397
+
```
398
+
366
399
## Static file authorization
367
400
368
401
:::moniker range=">= aspnetcore-9.0"
@@ -1014,6 +1047,57 @@ If [`staticAssetsManifestPath`](xref:Microsoft.AspNetCore.Builder.StaticAssetsEn
1014
1047
1015
1048
* Place code files, including `.cs` and `.cshtml`, outside of the app project's [web root](xref:fundamentals/index#web-root). A logical separation is therefore created between the app's client-side content and server-based code. This prevents server-side code from being leaked.
1016
1049
1050
+
## MSBuild properties
1051
+
1052
+
The following tables show the static files MSBuild properties and metadata descriptions.
`CompressionIncludePatterns` | Semicolon-separated list of file patterns to include for compression.
1058
+
`CompressionExcludePatterns` | Semicolon-separated list of file patterns to exclude from compression.
1059
+
`EnableDefaultCompressionFormats` | Enables default compression formats (Gzip and Brotli).
1060
+
`BuildCompressionFormats` | Compression formats to use during build.
1061
+
`PublishCompressionFormats` | Compression formats to use during publish.
1062
+
`DisableBuildCompression` | Disables compression during build.
1063
+
`CompressDiscoveredAssetsDuringBuild` | Compresses discovered assets during build.
1064
+
`BrotliCompressionLevel` | Compression level for the Brotli algorithm.
1065
+
`LinkAlternativeRepresentationsToOriginalResource` | How to link compressed assets to original resources.
1066
+
`StaticWebAssetBuildCompressAllAssets` | Compresses all assets during build, not just assets discovered or computed during a build.
1067
+
`StaticWebAssetPublishCompressAllAssets` | Compresses all assets during publish, not just assets discovered or computed during a build.
1068
+
1069
+
Property | Description
1070
+
--- | ---
1071
+
`StaticWebAssetBasePath` | Base URL path for all the assets in a library.
1072
+
`StaticWebAssetsFingerprintContent` | Enables content fingerprinting for cache busting.
1073
+
`StaticWebAssetFingerprintingEnabled` | Enables fingerprinting feature for static web assets.
1074
+
`StaticWebAssetsCacheDefineStaticWebAssetsEnabled` | Enables caching for static web asset definitions.
1075
+
`StaticWebAssetBuildManifestPath` | Path to the build manifest JSON file.
1076
+
`StaticWebAssetsBuildManifestCacheFilePath` | Path to the build manifest cache file.
1077
+
`StaticWebAssetEndpointExclusionPattern` | Pattern for excluding endpoints.
1078
+
1079
+
Item group | Description | Metadata
1080
+
--- | ---
1081
+
`StaticWebAssetContentTypeMapping` | Maps file patterns to content types and cache headers for endpoints. | Pattern, Cache
1082
+
`StaticWebAssetFingerprintPattern` | Defines patterns for applying fingerprints to static web assets for cache busting. | Pattern, Expression
1083
+
1084
+
Metadata Descriptions:
1085
+
1086
+
***Pattern**: A glob pattern used to match files. For `StaticWebAssetContentTypeMapping`, it matches files to determine their content type (for example, `*.js` for JavaScript files). For `StaticWebAssetFingerprintPattern`, it identifies multi-extension files that require special fingerprinting treatment (for example, `*.lib.module.js`).
1087
+
1088
+
***Cache**: Specifies the `Cache-Control` header value for the matched content type. This controls browser caching behavior (for example, `max-age=3600, must-revalidate` for media files).
1089
+
1090
+
***Expression**: Defines how the fingerprint is inserted into the filename. The default is `#[.{fingerprint}]`, which inserts the fingerprint before the extension.
1091
+
1092
+
The following table describes the runtime configuration options.
1093
+
1094
+
Configuration key | Description
1095
+
--- | ---
1096
+
`ReloadStaticAssetsAtRuntime` | Enables dev-time hot reloading of static assets: serves modified web root (`wwwroot`) files (recomputes `ETag`, recompresses if required) instead of build-time manifest versions. Defaults to enabled only when serving a build manifest unless explicitly set.
1097
+
`DisableStaticAssetNotFoundRuntimeFallback` | When `true`, suppresses the fallback endpoint that serves newly added files not present in the build manifest. When `false` or absent, a file-exists-checked `{**path}` fallback (GET/HEAD) logs a warning and serves the file with a computed `ETag`.
1098
+
`EnableStaticAssetsDevelopmentCaching` | When `true`, preserves the original `Cache-Control` headers on asset descriptors. When `false` or absent, rewrites `Cache-Control` headers to `no-cache` to avoid aggressive client caching during development.
1099
+
`EnableStaticAssetsDevelopmentIntegrity` | When `true`, keeps integrity properties on asset descriptors. When `false` or absent, removes any integrity property to prevent mismatches when files change during development.
0 commit comments