260

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(/.*)?"});
    
4
  • 4
    Your regular expression should be '\.' for the dot in favicon.ico, not just '.'. Sep 6, 2012 at 3:41
  • 1
    This is what I use (it takes @NathanAldenSr's advice into account, supports both forward and back slashes, and also allows for favicon.png files): routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*[/\\])?favicon\.((ico)|(png))(/.*)?" }); Jun 23, 2015 at 21:28
  • @BrainSlugs83 - You solution added the missing piece for me which was the slashes...Here is a .NET vNext version (beta-5). routes.MapRoute("IgnoreFavicon", "{*favicon}", new { favicon = @"(.*[/\\])?favicon\.((ico)|(png))(/.*)?" }); Dec 6, 2016 at 13:45
  • Correction, vNext Ignore Route routes.MapRoute("Ingore", "{*favicon}", new { }, new { favicon = @"(.*[/\\])?favicon\.((ico)|(png))(/.*)?" }); Notice thew new {}, Dec 7, 2016 at 15:34

9 Answers 9

235

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"/>
3
  • 3
    True. My answer was illustrating the "generic" version (HTML), so anyone could modify to suit their choice of framework/language :)
    – Chris
    Apr 6, 2012 at 8:29
  • 2
    @Diego - Yes, rel="SHORTCUT ICON" is non-standard implementation used by Internet Explorer. The space is a separator character in W3C standards (see developer.mozilla.org/en-US/docs/Web/HTML/Element/link). In browsers other than Internet Explorer the correct syntax is <link rel="icon" href="path/to/favicon.ico"> - see jonathantneal.com/blog/understand-the-favicon for more. Obviously you can use the Razor or Web Forms syntax for the path to the icon as above.
    – pwdst
    Jun 28, 2013 at 9:54
  • 3
    With MVC 4 you won't need the Url.Content in this case as that will be handled automatically, you can just use; <link rel="icon" href="~/content/favicon.ico"/> Oct 7, 2015 at 23:51
209

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"/>

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]-->
5
  • 25
    You need to use @Url.Content to generate the link so it works in all environments. See AlexC's response Mar 27, 2012 at 15:59
  • 5
    True, for ASP.NET. My answer illustrates the "generic" version (plain HTML), so anyone could modify to suit their choice of framework/language :)
    – Chris
    Apr 6, 2012 at 8:29
  • It is important to note that IE10 does not support conditional comments, so this will not work for users with that browser. A solution would be to add your favicon to the root directory of your site, which IE10 will pick up automatically if it doesn't find a link for a favicon anywhere. Jun 27, 2013 at 8:39
  • 2
    IE11 Understand PNG favicon and don't need to conditional statement. You should see this good article: jonathantneal.com/blog/understand-the-favicon
    – QMaster
    Aug 25, 2014 at 9:12
  • Thank you dude! Bottom part really helps me work with png, it just needs to be at the top of the page, masterpage or layout.
    – Alper
    May 9, 2019 at 13:02
24

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.

2
  • 2
    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? Jan 28, 2009 at 12:09
  • i think you're right, icons for bookmarks only work sometimes, I've never figured out why, maybe this is the case Jan 28, 2009 at 14:10
7

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.

2
  • 1
    I agree. It is in the root folder and should not be anywhere else... There is no need for a HTML Tag for the favicon if it is in the root folder. Works with all common browsers...
    – Vinz
    May 20, 2015 at 10:48
  • At first, this seemed like absolute bull to me, but apparantly there is no way around it, our logging keeps showing 404 errors from different user agents. Sep 20, 2016 at 8:14
5

None of the above worked for me. I finally solved this problem by renaming favicon.ico to myicon.ico, and reference it in the head <link rel="icon" href="~/myicon.ico" type="image/x-icon" />

1
  • Adding to this, on .NET 6 and Blazor Server this also works. I don't know why ASP.NET just won't allow us to replace the default favicon.ico with another file. Also, any arbitrary valid name will work for the name file, make sure the extension, type and the actual file is correctly an .ico file. Nov 24, 2021 at 20:20
2

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.

2

All you need to do is to add app.UseStaticFiles(); in your startup.cs -> public void Configure(IApplicationBuilder app, IHostingEnvironment env).

ASP.net core provides an excellent way to get static files. That is using the wwwroot folder. Please read Static files in ASP.NET Core.

Using the <Link /> is not a very good idea. Why would someone add the link tag on each HTML or cshtml for the favicon.ico?

0

Use this instead of just the favicon.ico which tends to search in for the fav icon file

> <link rel="ICON" 
> href="@System.IO.Path.Combine(Request.PhysicalApplicationPath,
> "favicon.ico")" />

Use the requested path and combine with the fav icon file so that it gets the accurate address which its search for

Using this solved the Fav.icon error which is raised always on Application_Error

0

Found that in .Net Core, placing the favicon.ico in /lib rather than wwwroot fixes the issue

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.