In .NET resource files, various types of content can be inserted – from plain text to images, the latter being represented as base64 encoded byte array (at least it looks like that in the resx file).
My purpose is to be able to list the resources within the application and to display additional information like the content (MIME) type, size (in bytes) and etc. I need to group by and sort/filter the resources by the content type.
My question is – is it possible to retrieve the proper MIME type of the resource item. For instance if I have stored a JPG image within the application resources, I’d like to have its content type as image/jpeg. If I have stored a JSON text, I’d like to have its MIME type saying application/json.
Is it possible to do this by the means of the .NET standard resource management capabilities?
I have read a number of related questions for MIME type retrieval. Most of the situations were related to working with a physical file. In the resources scenario, however, this is not always the case, because getting a resource by name returns an object that can be almost anything. Additionally, determining mime-types based on the binary representation of an object has additional notable drawbacks 1) it does not guarantee correctness for similar types (
.docxfiles might be wrongly recognized asapplication/zip) or to cause performance degradation as they require to process the objects bytes.So, I figured that the one who defines the resources is responsible for what content type they are representing. Therefore I decided to define mime-type mapping based on resource set name and resource name which is part of the application configuration. This will cause additional effort to add content-type description as new resources are added, but is easy enough to manage and does not require rebuilding the application.
For example a custom configuration section with the following contents could do the job:
The
defaultContentTypeattribute determines the content type returned for all unmapped resources. Since most items in resource files are strings, I decided thattext/plainwill ease my job as I will need to describe non-textual resources only. Grouping into resource sets allows for uniquely describing a resource item from a single resx file.