Changing website favicon dynamically

前端 未结 15 1495
温柔的废话
温柔的废话 2020-11-22 02:17

I have a web application that\'s branded according to the user that\'s currently logged in. I\'d like to change the favicon of the page to be the logo of the private label,

15条回答
  •  醉梦人生
    2020-11-22 02:44

    I would use Greg's approach and make a custom handler for favicon.ico Here is a (simplified) handler that works:

    using System;
    using System.IO;
    using System.Web;
    
    namespace FaviconOverrider
    {
        public class IcoHandler : IHttpHandler
        {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/x-icon";
            byte[] imageData = imageToByteArray(context.Server.MapPath("/ear.ico"));
            context.Response.BinaryWrite(imageData);
        }
    
        public bool IsReusable
        {
            get { return true; }
        }
    
        public byte[] imageToByteArray(string imagePath)
        {
            byte[] imageByteArray;
            using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
            {
            imageByteArray = new byte[fs.Length];
            fs.Read(imageByteArray, 0, imageByteArray.Length);
            }
    
            return imageByteArray;
        }
        }
    }
    

    Then you can use that handler in the httpHandlers section of the web config in IIS6 or use the 'Handler Mappings' feature in IIS7.

提交回复
热议问题