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

前端 未结 5 2049
醉话见心
醉话见心 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 01:55

    Cross-posting from Why would Reflection search suddenly not find anything?

    The good news is that the MimeMapping class and its GetMimeMapping method seem like they might be made public in .NET 4.5.

    However, this means that the code given in the above answer would break, since it’s only searching for GetMimeMapping among NonPublic methods.

    To ensure compatibility with .NET 4.5 (but preserve functionality in .NET 4.0 and earlier), change…

    getMimeMappingMethod = t.GetMethod("GetMimeMapping", 
        BindingFlags.Static | BindingFlags.NonPublic);
    

    …to:

    getMimeMappingMethod = t.GetMethod("GetMimeMapping",
        BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
    

提交回复
热议问题