Home >Backend Development >C++ >How to Determine File Extension MIME Types in ASP.NET and Other Frameworks?

How to Determine File Extension MIME Types in ASP.NET and Other Frameworks?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-09 17:02:41202browse

How to Determine File Extension MIME Types in ASP.NET and Other Frameworks?

Get the MIME type corresponding to the file extension

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)
  • MimeTypes NuGet Package
  • MimeMappings files from the .NET Framework reference source

.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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn