Simon_Weaver
Simon_Weaver

Reputation: 145950

Serving favicon.ico in ASP.NET MVC

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

I am currently doing the following:

I have two questions:

Upvotes: 261

Views: 153843

Answers (9)

Dimitris Kouris
Dimitris Kouris

Reputation: 1

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

Upvotes: 0

Ripal Barot
Ripal Barot

Reputation: 706

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?

Upvotes: 2

Tracy Zhou
Tracy Zhou

Reputation: 734

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

Upvotes: 5

AlexC
AlexC

Reputation: 10756

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

Upvotes: 236

Tribhuvan Patel
Tribhuvan Patel

Reputation: 1

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

Upvotes: 0

Carles Company
Carles Company

Reputation: 7216

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.

Upvotes: 2

Chris
Chris

Reputation: 6638

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]-->

Upvotes: 209

dmajkic
dmajkic

Reputation: 3488

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.

Upvotes: 7

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.

Upvotes: 26

Related Questions