Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For static assets included in an ASP.NET Core app’s build or publish output, use MapStaticAssets: its build-time pipeline prepares compressed representations rather than compressing a file on each request. Use UseStaticFiles for files outside that asset pipeline, such as files served through a custom provider. Adding UseStaticFiles alone does not make static files negotiate Brotli or gzip.

Choose the serving approach that matches your assets

Approach Best fit Compression behavior Other considerations
MapStaticAssets Assets known to the build or publish pipeline, such as the app’s static web assets and assets from referenced projects. Microsoft documents gzip precompression at build time and both gzip and Brotli precompression during publish. The pipeline can also provide content fingerprints, ETags, and immutable-cache metadata.
UseStaticFiles Files outside the build-time asset graph, including custom disk locations, custom file providers, and embedded resources. Static File Middleware does not itself compress static files or negotiate precompressed variants. Use it when the source or provider requires it; plan separately for compression and cache behavior.
Response Compression Middleware Responses that should be compressed at request time, where the middleware and its configured providers apply. Negotiates based on the request’s Accept-Encoding; Brotli is preferred when supported, with gzip as fallback. The response identifies the chosen encoding and varies on Accept-Encoding. Middleware order, MIME types, payload size, and HTTPS security all matter.

MapStaticAssets combines build- or publish-time asset information with a runtime component that uses it to serve files efficiently. It is therefore the natural choice for assets the app already knows about. The exact compression behavior depends on the stage: Microsoft documents gzip during development/build and gzip plus Brotli during publish.

These approaches solve different problems. Build-time asset compression prepares static representations ahead of requests. Response Compression Middleware applies runtime compression to eligible responses. Static File Middleware serves files but does not create compressed versions just because a matching .br or .gz file exists.

How request-time compression negotiation works

With Response Compression Middleware active, the browser advertises supported encodings in Accept-Encoding. When Brotli is supported, the middleware prefers it; gzip is the fallback. The server marks the selected representation with Content-Encoding and adds Vary: Accept-Encoding so caches distinguish compressed and uncompressed responses.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The default compression providers are Brotli and gzip, unless the application explicitly replaces the provider collection. Middleware order also matters: place UseResponseCompression before middleware that generates or compresses the responses it should handle. This is runtime negotiation, not a way to make UseStaticFiles automatically select sibling .br or .gz files.

Configure the pipeline for the files you serve

  1. For ordinary app assets: use MapStaticAssets for files handled by the build/publish static-asset pipeline. Confirm the target framework supports the API before adopting it; its availability is framework-version dependent.
  2. For external or custom sources: retain UseStaticFiles when files come from a location or provider outside that pipeline. Do not assume the middleware will compress them or negotiate precompressed siblings.
  3. For runtime-compressed responses: register response compression services and configure any needed providers and MIME types. In the request pipeline, put UseResponseCompression before middleware whose responses should be compressed.
  4. For verification: request the same asset with and without Accept-Encoding values such as br and gzip. Inspect Content-Encoding and Vary in the response using browser developer tools or an HTTP client.
  5. For caching: use fingerprinted URLs or an equivalent cache-invalidation strategy so a deployment cannot leave clients using stale asset bytes.

Check whether compression is worthwhile and safe

  • Consider MIME type: limit runtime compression to suitable content types rather than enabling it indiscriminately.
  • Check payload size: Microsoft warns that compressing small files can make the result larger. Test representative assets instead of assuming every file benefits.
  • Account for HTTPS security: Microsoft documents security considerations for enabling compression over HTTPS. Review those risks against the responses and data your application serves.
  • Keep cache variants distinct: when request-time negotiation selects encodings, Vary: Accept-Encoding is important for caches that might otherwise serve the wrong representation.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why a static file may not be served as Brotli

  • Only UseStaticFiles is configured: this middleware does not compress static files. A pre-existing .br file is not, by itself, evidence that the middleware will negotiate and serve it for a request to the original asset URL.
  • The asset is outside the build-time pipeline: MapStaticAssets is intended for assets known to that pipeline. Use the appropriate file provider for external sources and arrange compression behavior separately.
  • Request-time middleware is absent or ordered too late: for runtime compression, confirm UseResponseCompression is configured before the middleware producing the response.
  • The request and response do not show negotiation: send an Accept-Encoding header that includes Brotli, then inspect whether the response contains Content-Encoding: br and Vary: Accept-Encoding. If Brotli is not supported by the client, gzip may be selected instead.

Microsoft’s guidance is explicit: “Static files aren’t compressed by static file middleware.” Treat a missing Brotli response as a question of which middleware serves the asset and whether build-time preparation or runtime negotiation applies—not simply whether a compressed file exists beside it.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.