views:

3695

answers:

4

What is the final/best recommendation for how to serve favicon.ico in ASP.NET MVC?

I am currently doing the following :

  • Adding an entry to the very beginning of my RegisterRoutes method :

    routes.IgnoreRoute("favicon.ico");
    
  • Placing favicon.ico in the root of my application (which is also going to be the root of my domain).

I have two questions :

  • Is there no way to put favicon.ico somewhere other than the root of my application. Its pretty icky being right there at the same level as Content and Controllers.
  • Is this IgnoreRoute("favicon.ico") statement sufficient - or should I also do the following as discussed in a blog post from Phil Haack. I'm not aware of ever having seen a request to favicon.ico in any directory other than the root - which would make this unnecessary (but its nice to know how to do it).

    routes.IgnoreRoute("{*favicon}", new {[email protected]"(.*/)?favicon.ico(/.*)?"});
    
A: 

1) You can put your favicon where you want and add this tag to your page head

<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />

although some browsers will try to get the favicon from /favicon.ico by default, so you should use the IgnoreRoute.

2) If a browser makes a request for the favicon in another directory it will get a 404 error wich is fine and if you have the link tag in answer 1 in your master page the browser will get the favicon you want.

Eduardo Campañó
browsers will go and look for the .ico file if you bookmark the site - so this doesn't help that. but i'm wondering if the browser remembers that. i just know that in Fiddler sometimes I see a whole slew of icons being retrieved. perhaps that is google toolbar though?
Simon_Weaver
i think you're right, icons for bookmarks only work sometimes, I've never figured out why, maybe this is the case
Eduardo Campañó
A: 

I think that favicon.ico should be in root folder. It just belongs there.

If you want to servere diferent icons - put it into controler. You can do that. If not - just leave it in the root folder.

dmajkic
+12  A: 

Placing favicon.ico in the root of your domain only really affects IE5, IIRC. For more modern browsers you should be able to include a link tag to point to another directory:

<link rel="SHORTCUT ICON" href="http://www.mydomain.com/content/favicon.ico"/&gt;

You can also use non-ico files for browsers other than IE, for which I'd maybe use the following conditional statement to serve a PNG to FF,etc, and an ICO to IE:

<link rel="icon" type="image/png" href="http://www.mydomain.com/content/favicon.png" />
<!--[if IE]>
<link rel="shortcut icon" href="http://www.mydomain.com/content/favicon.ico" type="image/vnd.microsoft.icon" />
<![endif]-->
Chris
+1  A: 

It should also be possible to create a controller that returns the ico file and register the route /favicon.ico to point to that controller.

Carles