MIME type warning in chrome for png images

 ̄綄美尐妖づ 提交于 2019-11-29 20:24:35

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>

The quickest way around the spam that I've found is to use the CTRL key to select Errors, Warnings and Debug instead of all.

All:

Errors, Warnings and Debug:

I've solved this problem by enabling Static Content in Control Panel > Programs and Features > Turn Windows features on or off > IIS components > World Wide Web Services > Common HTTP Features

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!