Skip to content

[API Proposal]: System.Net.Mime.MediaTypeMap #121017

Closed

Description

@stephentoub

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

Activity

  1. added
    api-suggestion
    on Oct 23, 2025
  2. added
    untriaged
    on Oct 23, 2025
  3. added
    needs-area-label
    on Oct 23, 2025
  4. MihuBot commented on Oct 23, 2025

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  5. added and removed
    needs-area-label
    on Oct 23, 2025
  6. dotnet-policy-service commented on Oct 23, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  7. added this to the 11.0.0 milestone on Oct 23, 2025
  8. removed
    untriaged
    on Oct 23, 2025
  9. added
    untriaged
    on Oct 23, 2025
  10. removed
    untriaged
    on Oct 23, 2025
  11. MichalPetryka commented on Oct 23, 2025

    @MichalPetryka
    Contributor

    // optional
    public static IReadOnlyDictionary<string, string> MediaTypeToExtension { get; }
    public static IReadOnlyDictionary<string, string> ExtensionToMediaType { get; }

    Why not FrozenDictionary?

  12. stephentoub commented on Oct 24, 2025

    @stephentoub
    MemberAuthor

    // optional
    public static IReadOnlyDictionary<string, string> MediaTypeToExtension { get; }
    public static IReadOnlyDictionary<string, string> ExtensionToMediaType { get; }

    Why not FrozenDictionary?

    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.

  13. added
    api-ready-for-review
    and removed
    api-suggestion
    on Nov 4, 2025
  14. self-assigned this
    on Nov 4, 2025
  15. self-assigned this
    on Nov 4, 2025
  16. fredrikhr commented on Nov 18, 2025

    @fredrikhr
    Contributor

    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.

  17. bartonjs commented on Nov 18, 2025

    @bartonjs
    Member

    Video

    • To maximize usability, we went with the non-Try approach, and overloaded with string inputs
    • Changed the parameter name for MIME -> Extension from extension to pathOrExtension, the implementation will route through Path.GetExtension (or equivalent)
    namespace System.Net.Mime;
    
    public static class MediaTypeMap
    {
        // foo.pdf, .pdf, or pdf => application/pdf
        public static string? GetMediaType(string pathOrExtension);
        public static string? GetMediaType(ReadOnlySpan<char> pathOrExtension);
    
        // application/pdf => .pdf
        public static string? GetExtension(string mediaType);
        public static string? GetExtension(ReadOnlySpan<char> mediaType);
    }
  18. added
    api-approved
    and removed
    api-ready-for-review
    on Nov 18, 2025
  19. locked and limited conversation to collaborators on Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Net

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions