Serving favicon.ico in ASP.NET MVC

Simon_Weaver picture Simon_Weaver · Jan 28, 2009 · Viewed 129.1k times · Source

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 the favicon.ico somewhere other than the root of my application. It's 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 it's good to know how to do it).

    routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});
    

Answer

AlexC picture AlexC · Mar 27, 2012

I agree with the answer from Chris, but seeing this is a specific ASP.NET MVC question it would be better to use either Razor syntax:

<link rel="icon" href="@Url.Content("~/content/favicon.ico")"/>

Or traditionally

<link rel="icon" href="<%= Url.Content("~/content/favicon.ico") %>"/>

rather than

<link rel="icon" href="http://www.mydomain.com/content/favicon.ico"/>