Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIME type warning in chrome for png images

I encountered this while running an ASP.NET WebForms app using the ASP.NET Development Server.

I suspect something similar will happen if you use IIS Express as your server as well (VS 2010 SP1).

I 'resolved' my problem locally by editing the project settings (under Web) and changed from the ASP.NET Development Server to IIS on my local machine. I can see that PNG was already defined correctly as an image MIME type and indeed when I hit my local IIS server it's serving up the file with the correct type.


This warning is telling you that your web server isn't configured to send the correct MIME type meta data for PNG images. You should probably consult the administrator for your web server and ask them to set the correct MIME mapping


I added types like this in .htaccess (AddType image/type extention) i.e.

AddType image/png cur
AddType image/svg+xml svg svgz

Ofcourse above solutions are perfect. Just to avoid warnings and for a clean console I done following change in my code. (that too only for ASP.NET Development Server) I written a extra handler for this:

PNGHandler.cs

class PNGHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    { 
       if(context.Request.HttpMethod == "GET") 
       {
             string requestedFile = context.Server.MapPath(context.Request.FilePath);
             FileInfo fileinfo = new FileInfo(requestedFile);
             string contentType = "";
             if (fileinfo.Exists && fileinfo.Extension.Remove(0, 1).ToUpper() == "PNG")
             {
                   contentType = "image/png";
                   context.Response.ContentType = contentType;
                   context.Response.TransmitFile(requestedFile);
                   context.Response.End();
              }
         }
    }
}

And added Http Handler in web.config under system.web

<system.web>
 <httpHandlers>
 <add path="*.png" verb="*" type="PNGHandler" />
 </httpHandlers>
</system.web>