Why is Chrome searching for my favicon.ico when I serve up a file from ASP.NET MVC?

夙愿已清 提交于 2019-11-30 11:49:00

This has nothing to do with MVC. I am using webforms with a custom built log service and I stumbled upon this post wondering why I had continuous 'File does not exist' errors in my logs. This is locally on my development machine, I have no favicon.ico files in my projects, and I have tried IE, Firefox and Google trying to see which browser is the guilty party.

Every request from Google Chrome to my apps makes a request for a favicon.ico. I had to start logging browser locally to determine that it was in fact googles browser that is the culprit. I'd contact google if it bothers you. I just wanted to make sure it wasn't some new trojan infecting my chrome.

The actual answer: It's a known, verified bug. *(recently fixed!... maybe?)

Looks like a known, longstanding issue with Chrome: http://crbug.com/39402

If you want it fixed sooner, star the issue. More people starring the issue will likely increase its priority and possibly get it fixed faster.


****UPDATE 1***: As of May 15 of this year (2013)--four years after this question was asked--it looks like the issue has been fixed in version 29: http://crbug.com/39402#c47

Feel free to undo all your hacks and workarounds. :]

****UPDATE 2 (2015-01)***: This is apparently still an issue for some users, according to the same issue link. :/

Do you have a favicon? If not, perhaps that's why Chrome is attempting to find it every time for your website. For google it already has the favicon cached.

one thing you could do is have MVC ignore any request for *.ico so that you don't get any exceptions while debugging.

Should be something like this:

routes.MapRoute("ignore-favicon", "{*path}", null, new {path = ".*/favicon\\.ico"});

That URL pattern matches everything, but then we constrain it to only match anything ending in favicon.ico. (I haven't tested this)

I ran into this problem a while back and got around it by ignoring the specific route by adding

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

into the RegisterRoutes method in Global.asax.

It appears for me that Chrome requests a favicon for its own tabs - I kept getting 404s (because my favicon is somwhere else and my pages know it) till I did some tests and realized it was Chrome making direct requests to the favicon file. No real fix except making a rewrite to the real file I guess

Denis Volovik

You can add something like this within your web.config file to make sure that the favicon.ico is cached on the client and is not being requested every time.

<location path="favicon.ico">
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Cache-Control" value="public, max-age=31536000" />
             </customHeaders>
        </httpProtocol>
    </system.webServer>
</location>

You can/should do the same for any images / .js and css files

You should set the Expires header to tell the browser how long it should use its local copy.

If you check your project setting it says default icon somewhere. Remove that?

Chrome browser could work with Google site in another way than with any other site, so, at first, I would recommend to check if it looks for favicon.ico every time somewhere else, for example, on StackOverflow.

I would also check if Firefox does the same with your site. I think favicon.ico should be requested only one time per browser run even if it isn't present on site. This could be bug in Chrome version you use.

Colin Desmond

This SO question/answer explains how to serve the Favicon to the browser by using routes.

Its important to put in an ICON link into your masterpage or some browsers will try to find favicon.ico for all directories and not just globally once per done.

 <link rel="SHORTCUT ICON" href="<%= Url.Content("~/content/images/rr-favicon.ico") %>"/>

It seems google toolbar is the guilty party judging by my logs (and IE6 of course). They both will make requests for directories other than the root

 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
 Mozilla/4.0 (compatible; GoogleToolbar 6.2.1910.1554; Windows 6.0; MSIE 8.0.6001.18828)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!