Today I was looking at a way to set the correct MIME type for a response based on the file extension.
What is a MIME type?
A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) is a standard that indicates the nature and format of a document, file, or assortment of bytes. It is defined and standardized in IETF's RFC 6838.
How do you set the MIME type in ASP.NET Core?
In ASP.NET core you can set the MIME type of the file you return through the contenttype property on the File method of the ControllerBase class:
How to get the MIME type from a file extension?
My first idea was to write a switch statement with the list of file extension and the corresponding MIME types. But this turned out not be necessary as the ASP.NET Core team already provides you this functionality out-of-the-box through the FileExtensionContentTypeProvider: