MIME type warning in chrome for png images

后端 未结 6 1908
栀梦
栀梦 2020-12-23 08:50

Just ran my site in chrome and suprisingly it comes up with this warning for each of my .png images:

Resource interpreted as image but transferred with MIME          


        
6条回答
  •  北海茫月
    2020-12-23 09:10

    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

    
     
     
     
    
    

提交回复
热议问题