6

I just add this in the head of my _Layout.cshtml.

<head>  
    <link rel="shortcut icon" type="image/ico" href="~/Images/favicon.ico">
</head>` 

the icon is showing but only in homepage, what is the reason for that?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Kendall H.
  • 461
  • 3
  • 9
  • 21
  • 1
    Are all your pages using the same layout. – pool pro May 31 '16 at 05:13
  • 2
    also for reference, in MVC try to use the razor syntax so it works in all environments. – pool pro May 31 '16 at 05:19
  • Thank you for the help Sir. – Kendall H. May 31 '16 at 05:36
  • 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. if you need to use more than one layout file i suggest using your master layout file and having any layout file use this for your meta tags and other information that does not change. or make sure the meta tag is set in all the layout files you use. – pool pro May 31 '16 at 05:58

2 Answers2

7

In general, it is a good practice to put all favicon-related files in the root directory of the web site. Some browsers will always check for favicon.ico in the root folder.

<head>  
    <link rel="shortcut icon" type="image/ico" href="~/favicon.ico">
</head>

Also, take a look at these posts

Is putting your favicon.ico file in a non-root path a bad idea?

Serving favicon.ico in ASP.NET MVC

Community
  • 1
  • 1
Nkosi
  • 235,767
  • 35
  • 427
  • 472
2

Rather than moving the favicon.ico file into the root, you can easily add a rewrite rule using the IIS IRL rewrite module, which will make it act like it is in the root directory even though it is where you would rather keep it.

Simply install the rewrite module, and drop this into your root web.config file.

<configuration>
  <system.webServer>
    <rewrite>
        <rule name="Rewrite favicon.ico location from root to physical">
          <match url="^favicon\.ico$"/>
          <action type="Rewrite" url="images/favicon.ico"/>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Of course, you can also specify the actual location for the file in your _Layout.cshtml file for those browsers that will respect the shortcut icon tag.

<head>  
    <link rel="shortcut icon" type="image/ico" href="@Url.Content("~/Images/favicon.ico")">
</head>
NightOwl888
  • 55,572
  • 24
  • 139
  • 212