Repository navigation
Closed
Labels
Milestone
Description
Background and motivation
It's reasonably common to need to get a MIME / media type for a file extension, or vice versa. The core .NET libraries don't include any exposed mapping, so developers end up needing to re-create the subset of the mapping that's relevant to their current needs. Java supports this with URLConnection.getFileNameMap, Go supports this with its built-in mime.TypeByExtension, Swift has UTType... .NET should have this available as well. It would support all IANA mappings plus very common ones like those from Apache mime.types or mime-db.
API Proposal
namespace System.Net.Mime; // existing namespace for MediaTypeNames
public static class MediaTypeMap // members could alternatively be added to MediaTypeNames if we wanted to avoid the extra type
{
public static bool TryGetMediaType(ReadOnlySpan<char> extension, [NotNullWhen(true)] out string? mediaType);
public static bool TryGetExtension(ReadOnlySpan<char> mediaType, [NotNullWhen(true)] out string? extension);
}API Usage
UriContent image = new(uri, MediaTypeMap.TryGetMediaType(Path.GetExtension(uri.AbsolutePath), out var mediaType) ? mediaType : "image/*"));Alternative Designs
namespace System.Net.Mime; // existing namespace for MediaTypeNames
public static class MediaTypeMap // members could alternatively be added to MediaTypeNames if we wanted to avoid the extra type
{
public static string? GetMediaType(ReadOnlySpan<char> extension);
public static string? GetExtension(ReadOnlySpan<char> mediaType);
}namespace System.Net.Mime; // existing namespace for MediaTypeNames
public static class MediaTypeMap // members could alternatively be added to MediaTypeNames if we wanted to avoid the extra type
{
// optional, on top of the functions
public static IReadOnlyDictionary<string, string> MediaTypeToExtension { get; }
public static IReadOnlyDictionary<string, string> ExtensionToMediaType { get; }
}Risks
- We may not be "complete" in the opinion of some consumers and may have a trickle of requests for adding new entries. Those new entries would not require public API, though; they'd just be updating the internal map.
EDIT: updated after internal discussion
Reacted by David Lee, Ahmet Ibrahim Aksoy, nil4, Egor Bogatov, pedoc, campersau, Meir Blachman, Oron Feinerman, pierluc-mc, Christian Rondeau and 3 more
Activity
MihuBot commentedon Oct 23, 2025
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
dotnet-policy-service commentedon Oct 23, 2025
Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.
MichalPetryka commentedon Oct 23, 2025
Why not FrozenDictionary?
stephentoub commentedon Oct 24, 2025
The implementation could still be one. If we were to add such properties (I suggested they'd be optional), having them be interfaces gives us flexibility for their implementations.
fredrikhr commentedon Nov 18, 2025
Probably obvious, but it would be important to have a well-documented location that lists the mappings available by this API, with the .NET version when an entry was added.
I envision a common pitfall where a developer tests a given mapping at compile time on their up-to-date dev machine, but then realises in production that the same mapping is not (yet) available, because the production system is lagging one patch version behind (or sth.). Try-pattern helps with this, but a developer might easily be fooled into assuming a constant true for their particular use-case and therefore neglect the check.
bartonjs commentedon Nov 18, 2025
Video
Add System.Net.Mime.MediaTypeMap (#121776)