navigation
asp.net MVC

How to get websites favicon in mvc asp.net?

| | ASP , ASP-NET , CSharp , MVC

You can use HTML agility pack or XmlDocument and get favicon. The following code will bring the path of website’s icon.

  public JsonResult GetFavIcon(string term)
        {
            string htmlCode;
            try
            {
                using (WebClient client = new WebClient())
               {
                   htmlCode = client.DownloadString(term);
               }
                HtmlDocument doc = new HtmlDocument();
               doc.LoadHtml(htmlCode);
 
                if (doc.DocumentNode != null)
               {
                   var favicon = "/favicon.ico";
                   var el =doc.DocumentNode.SelectSingleNode("/html/head/link[@rel='shortcut icon' and@href]");
              }
                return Json(submit, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return null;
            }
        }