How can I query IIS for MIME Type Mappings?

后端 未结 4 414
情歌与酒
情歌与酒 2020-12-16 01:15

How can I programatically read IIS\'s MIME types? I\'d like to use them when I stream data to my clients using WCF.

Any tips, or API would be appreciated

4条回答
  •  悲哀的现实
    2020-12-16 01:27

    If using ASP.Net 4.5 or higher, you can use System.Web.MimeMapping.GetMimeMapping as follows:

    void Page_Init()
    {
        string[] extensions = new string[]
        {
            ".pdf",
            ".xls",
            ".xlsx",
            ".ppt",
            ".pptx",
            ".mp3",
            ".ogg",
            ".svg",
            ".pdf",
            ".png",
        };
    
        foreach (string extension in extensions)
        {
            string mimeType = MimeMapping.GetMimeMapping(extension);
            Response.Write(String.Format("{0} => {1}
    ", extension, mimeType )); } }

    Result:

    .pdf => application/pdf
    .xls => application/vnd.ms-excel
    .xlsx => application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    .ppt => application/vnd.ms-powerpoint
    .pptx => application/vnd.openxmlformats-officedocument.presentationml.presentation
    .mp3 => audio/mpeg
    .ogg => video/ogg
    .svg => image/svg+xml
    .pdf => application/pdf
    .png => image/png
    

提交回复
热议问题