Yannik
Yannik

Reputation: 1037

How to get favicon from url when website has no access to the internet

How can I get the favicon from ANY webpage (webpage has for example no access to the internet) without using a 3th party application like Google S2 Converter or something else in c#/asp.net?

I am a developer of an internal Web application; we generate in one of our page a table with dynamic links to other internal websites. We don’t want to download every possible icon from the internal websites and link them in the table. Is there a way to get the icon from a page dynamically?

example:

<table>
@foreach (var mylink in Model.links) 
{
   var faviconOfPage = //Get Url of favicon > example: favicon of subpage.mycompany.com
   <tr>
       <td><a href="@(mylink.url).ToString()">@mylink.name</a></td>
       <td><img src="@faviconOfPage"></td>
   </tr>
}
</table>

Note: The requested site is internal: no access from outside!

Thank you for helping me!

Upvotes: 1

Views: 2097

Answers (1)

CodeCaster
CodeCaster

Reputation: 151654

A favicon can be supplied through two ways: convention (located at /favicon.ico at the root of the site) or through the <link rel="shortcut icon" /> element.

Because the latter can be the case for every page, you need to request and parse every page to see whether that <link> element is present, and if so, download the file specified by the href attribute. If not, you can download the /favicon.ico from the host.

Since you need to do this per page, and since it can differ per request, you're going to need some caching. You can't request and parse N pages for every page of your web application containing N links, because that would make your application horribly slow.

So create some kind of background job that processes your links, downloads their favicons and stores them in a way so that you can associate icons with links.

Upvotes: 1

Related Questions