Fix ContentTypeProvider to respect custom content-type mappings precedence #50153
+52
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue where custom content-type mappings were being ignored in favor of built-in mappings in the Static Web Assets ContentTypeProvider.
Problem
In .NET SDK versions 9.0.300+, the
ContentTypeProvider
was prioritizing built-in mappings over custom mappings when both existed for the same file pattern. This prevented users from overriding default cache behavior for common file types like*.html
and*.js
.The issue occurred in the
TryGetMapping
method where built-in mappings were checked first:Since built-in mappings for
*.html
and*.js
haveCache = null
, they would always win over custom mappings that might specify cache headers like"no-cache"
or"max-age=3600"
.Solution
The fix swaps the lookup order to check custom mappings first:
This minimal one-line change ensures that:
Testing
Added comprehensive regression tests to verify:
*.html
mappings with cache settings override built-in mappings*.js
mappings with cache settings override built-in mappings.gz
)This restores the expected behavior from SDK versions 9.0.101–9.0.205 where user configuration properly overrode built-in defaults.
Fixes #50152.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.