Search code examples
c#asp.net-core-mvcmime-typessystem.web

Using MimeMapping in ASP.NET Core


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


Solution

  • The following code should work:

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