How do I get the MIME type of a file being requested in ASP.NET C#?

前端 未结 5 2029
醉话见心
醉话见心 2020-12-15 01:06

I would like to handle requests differently depending upon the MIME type. For example, I have PDF\'s, images and other media files that I would like to prohibit access to ba

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 02:00

    .NET's mime-type mappings are stored in the System.Web.MimeMapping class which offers the GetMimeMapping method.

    Prior to .NET 4.5, this class was marked as internal, and thus not available to your code. In that case the best you can do is steal the list, which you can get using Reflector and decompile the static constructor (cctor).

    If taking that approach, you may be better off simply creating a list of supported extensions and their mime type and storing it on a dictionary. (The list inside MimeMapping is a tad verbose)

提交回复
热议问题