Problem Description
How to get the MIME type associated with a file extension?
Solution
ASP.NET and other frameworks
In ASP.NET Core, you have the following options:
new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);
(VNext only) .NET Framework 4.5 and above
Use System.Web.MimeMapping.GetMimeMapping
Method:
<code class="language-csharp">string mimeType = MimeMapping.GetMimeMapping(fileName);</code>
Custom mapping added (reflection)
To add a custom mapping (using reflection), call the following code (Note : private fields are used):
<code class="language-csharp">MimeMapping._mappingDictionary.AddMapping(string fileExtension, string mimeType)</code>
The above is the detailed content of How to Determine File Extension MIME Types in ASP.NET and Other Frameworks?. For more information, please follow other related articles on the PHP Chinese website!