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
andControllers
. 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(/.*)?"});
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.
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.
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?Use this instead of just the favicon.ico which tends to search in for the fav icon file
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
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" />
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:
Or traditionally
rather than