Provides a lightweight and immutable MimeType value object to represent media types (MIME types) in .NET. Includes parsing, safe parsing, well-known application/* and image/* media types, and helpers to map between file extensions and media types.
$ dotnet add package PosInformatique.Foundations.MediaTypesPosInformatique.Foundations.MediaTypes provides
a lightweight way to represent media types (MIME types) in .NET.
It offers an immutable MimeType value object, a set of well-known media types, helpers for mapping between
file extensions and media types, and a few convenience extension methods.
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.MediaTypes
MimeType value object (type/subtype, e.g. application/json, image/png).string (Parse / TryParse).IFormattable and IParsable<T> for seamless integration with .NET APIsMimeType from a file extension (with or without leading dot).MimeType.application/* and image/* media types.IsPdf() and IsImage().using PosInformatique.Foundations.MediaTypes;
var json = MimeType.Parse("application/json");
Console.WriteLine(json.Type); // "application"
Console.WriteLine(json.Subtype); // "json"
if (MimeType.TryParse("image/png", out var png))
{
Console.WriteLine(png); // "image/png"
}
using PosInformatique.Foundations.MediaTypes;
var pdf = MimeTypes.Application.Pdf; // application/pdf
var docx = MimeTypes.Application.Docx; // application/vnd.openxmlformats-officedocument.wordprocessingml.document
var jpeg = MimeTypes.Image.Jpeg; // image/jpeg
using PosInformatique.Foundations.MediaTypes;
var pdfFromExt = MimeType.FromExtension(".pdf"); // application/pdf
var pngFromExt = MimeType.FromExtension("png"); // image/png
// Unknown extensions fall back to application/octet-stream
var unknown = MimeType.FromExtension(".unknown"); // application/octet-stream
using PosInformatique.Foundations.MediaTypes;
var pdf = MimeTypes.Application.Pdf;
var pdfExtension = pdf.GetExtension(); // ".pdf"
var webp = MimeTypes.Image.WebP;
var webpExtension = webp.GetExtension(); // ".webp"
using PosInformatique.Foundations.MediaTypes;
var mimeType = MimeTypes.Application.Pdf;
if (mimeType.IsPdf())
{
Console.WriteLine("This is a PDF document.");
}
var image = MimeTypes.Image.Png;
if (image.IsImage())
{
Console.WriteLine("This is an image type.");
}
var drawing = MimeTypes.Image.Dwg;
if (drawing.IsAutoCad())
{
Console.WriteLine("This is an AutoCAD drawing type.");
}
type/subtype.IEquatable<MimeType> and IParsable<MimeType>.string Type { get; }string Subtype { get; }static MimeType Parse(string s)static MimeType Parse(string s, IFormatProvider? provider)static bool TryParse(string? s, out MimeType? result)static bool TryParse(string? s, IFormatProvider? provider, out MimeType? result)static MimeType FromExtension(string extension)string GetExtension()Provides common media types and mapping helpers.
MimeTypes.Application
OctetStream (application/octet-stream)Pdf (application/pdf)Docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document)MimeTypes.Image
Bmp (image/bmp)Dxf (image/x-dxf)Dwg (image/x-dwg)Jpeg (image/jpeg)Png (image/png)Tiff (image/tiff)WebP (image/webp)bool IsAutoCad(this MimeType mimeType)bool IsImage(this MimeType mimeType)bool IsPdf(this MimeType mimeType)