c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
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;
            }
        }