NMimeType 1.0.2
dotnet add package NMimeType --version 1.0.2
NuGet\Install-Package NMimeType -Version 1.0.2
<PackageReference Include="NMimeType" Version="1.0.2" />
<PackageVersion Include="NMimeType" Version="1.0.2" />
<PackageReference Include="NMimeType" />
paket add NMimeType --version 1.0.2
#r "nuget: NMimeType, 1.0.2"
#:package NMimeType@1.0.2
#addin nuget:?package=NMimeType&version=1.0.2
#tool nuget:?package=NMimeType&version=1.0.2
NMimeType
Fast MIME type lookup for .NET applications.
The package resolves MIME types from file names, paths, or extensions using an embedded lookup table. It keeps lookups case-insensitive and indexes the table in memory so repeated reads are fast.
Why this package matters
MIME type detection shows up in file uploads, downloads, API responses, static file serving, email attachments, background processing, and storage pipelines. Getting it wrong can cause browsers to render files incorrectly, APIs to return poor metadata, or upload systems to accept files without enough classification.
This package keeps that lookup simple:
- No operating system dependency for the MIME table.
- No repeated JSON scanning during normal lookups.
- Case-insensitive extension matching.
- Works with file names, full paths, or raw extensions.
- Supports custom embedded MIME resources for product-specific file types.
- Targets older compatible libraries and current .NET applications.
Supported frameworks
netstandard2.0net8.0net9.0net10.0
net8.0, net9.0, and net10.0 use the built-in System.Text.Json. The netstandard2.0 target references the Microsoft System.Text.Json package.
Features
- Fast indexed lookups after the embedded table is loaded.
- Built-in MIME table with common document, image, audio, video, archive, web, app package, and source file extensions.
- Modern extensions such as
avif,heic,wasm,webmanifest,zst,br,yaml,toml,ndjson, andgeojson. Getfor simple lookups.TryGetfor unknown-safe lookups.GetOrDefaultfor fallback MIME values.Containsto check whether an extension is known.GetKnownTypesto inspect the loaded table.- Custom embedded JSON resources with override support.
Basic usage
using MimeType;
string mimeType = Mime.Get("report.pdf");
// application/pdf
string fromExtension = Mime.Get(".json");
// application/json
string fromPath = Mime.Get("/uploads/avatar.webp");
// image/webp
Fallback values
Use GetOrDefault when your pipeline needs a real MIME type even for unknown extensions.
string mimeType = Mime.GetOrDefault("upload.unknown");
// application/octet-stream
string customFallback = Mime.GetOrDefault("upload.unknown", "application/x-custom-fallback");
// application/x-custom-fallback
TryGet and Contains
Use TryGet when an extension may be unknown and you want to avoid treating an empty string as a result.
if (Mime.TryGet("video.webm", out var mimeType))
{
Console.WriteLine(mimeType);
}
bool supported = Mime.Contains("archive.zst");
Instance API
IMime mime = new Mime();
string value = mime["document.docx"];
string sameValue = mime.Get("document.docx");
List known types
IReadOnlyDictionary<string, string> knownTypes = Mime.GetKnownTypes();
foreach (var item in knownTypes)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
Custom MIME resources
You can add your own embedded JSON resources. Custom resources override the built-in table when the same extension appears in both.
Example JSON:
{
"custom": "application/x-custom",
"pdf": "application/pdf"
}
Marker type:
using MimeType;
[MimeTypeResourceName("Content")]
public class MyMimeTypes
{
}
Register the resource:
Mime.AddMimeResources(typeof(MyMimeTypes));
The JSON file should be embedded in the same assembly as the marker type. The attribute value should match the namespace or folder segment used in the embedded resource name.
Build
dotnet build MimeType.sln
Release package
The repository includes a GitHub Actions workflow that creates and publishes the NuGet package when a GitHub Release is published.
Before publishing a release, add this repository secret in GitHub:
NUGET_API_KEY: an API key from nuget.org with permission to push this package.
Release flow:
- Create a tag such as
v1.0.0. - Create and publish a GitHub Release for that tag.
- The workflow builds the solution, packs
NMimeType, uploads the.nupkgas a workflow artifact, and pushes it to nuget.org.
The package version is taken from the release tag. For example, v1.0.0 publishes package version 1.0.0.
Notes
- Extension lookup is case-insensitive.
- File names and paths are supported; the last extension is used.
- Unknown extensions return
string.EmptyfromMime.Get. - Prefer
Mime.TryGetorMime.GetOrDefaultwhen unknown extensions are expected.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Text.Json (>= 10.0.0)
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.