Azure MIME mapping

It turns out that Azure Websites have some MIME mapping problems that a default IIS installation has not.

I’ve been playing around with serving files through asp.net mvc. Basically, what I did was:

public ActionResult GetFile(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return HttpNotFound();
            }

            var fileName = MapFilePath(path);
            return File(fileName, System.Web.MimeMapping.GetMimeMapping(fileName)); 
        }

This works fine on my local machine (using IIS Express). Surprisingly, after publishing to Azure Website, I received a download alert instead of file content inside the browser. After some quick debugging, I found out that the file extension (.xhtml in this case) was being mapped to application/octet-stream instead of application/xhtml+xml. Interestingly, GetMimeMapping uses IIS MIME mapping: http://www.c-sharpcorner.com/Blogs/47150/. So, my local IIS has this extension mapped and Azure has not. This may be a cool feature, but in this case it just gets in my way. So, instead of adding mappings to my web.config, I decided to hardcode the mapping dictionary, using this little class: https://github.com/samuelneff/MimeTypeMap/blob/master/src/MimeTypeMap.cs This gist is similar, but it does not containt xhtml: https://gist.github.com/atifaziz/14553

Now, that’s better.