Using MimeMapping in ASP.NET Core

Sauron picture Sauron · Dec 7, 2015 · Viewed 19.9k times · Source

I'm trying to move my old mvc5 project to asp net core. Old code was:

public string ContentType
{
    get
    {
        if (!string.IsNullOrEmpty(FileName))
            return MimeMapping.GetMimeMapping(FileName);
        return null;
    }
}

Error is

The name 'MimeMapping' does not exist in the current context

enter image description here

Answer

Mark G picture Mark G · Mar 9, 2016

The following code should work:

string contentType;
new FileExtensionContentTypeProvider().TryGetContentType(FileName, out contentType);
return contentType ?? "application/octet-stream";