views:

1589

answers:

10

I have a controller in MVC serving up images from a database.

EDIT: This still happens if I serve up a file over completely standard means in MVC.

Every time I request my image, Google Chrome also searches for my favicon.ico.

To avoid unnecessary discussions about other things "I should also care about" let us assume I do not care for caching whatsoever in this example and I shall always return HTTP response 200 with the file.

In my controller I return the following:

return File(fileBytes, contentType);

After inspecting Fiddler 2, the following response is generated:

HTTP/1.1 200 OK
Cache-Control: public
Content-Type: image/gif
ETag: oYu19wKo+KEHkyxZQ2WXAA==
Server: Microsoft-IIS/7.0
X-AspNetMvc-Version: 1.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Tue, 16 Jun 2009 18:48:45 GMT
Content-Length: 29344

By comparison, this is the response in Fiddler from Google when I request (for the first time) the Google logo:

HTTP/1.1 200 OK
Content-Type: image/gif
Last-Modified: Wed, 07 Jun 2006 19:42:34 GMT
Date: Tue, 16 Jun 2009 18:50:54 GMT
Expires: Wed, 16 Jun 2010 18:50:54 GMT
Cache-Control: public, max-age=31536000
Server: gws
Content-Length: 8706
Age: 2

However, in Chrome after getting my image Chrome attempts to find my favicon.ico. It does not try this after requesting the Google logo.

Any ideas why this might be happening? From my understanding on HTML, the answer must be in the response header because surely that is all the client has to go on? Please correct me!

EDIT 2: It seems a lot of people have completely misunderstood the problem. The problem is not the lack of a favicon and the erroring requests in MVC - it's the problem of requesting a favicon when only an image is being loaded, with a content type of "IMAGE/JPEG", as opposed to a webpage with a content type of "TEXT/HTML"!!

+4  A: 

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.

Vilx-
Actually I just checked this out. I stuck a blank favicon.ico in there, but it repeats the request.
joshcomley
Does "blank" mean zero size?
Alexander Prokofyev
Yep, I just made an empty favicon.ico to test it out
joshcomley
Maybe try placing a real 16x16px icon there that Chrome can actually display. Otherwise it might be just discarding it.
Vilx-
I would also test with real icon file.
Alexander Prokofyev
A: 

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

DSO
A: 

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

mhenrixon
A: 

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.

Alexander Prokofyev
+3  A: 

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)

Haacked
A few have suggested this now - I don't want it to ignore favicon requests! I want to know why a request is being made in the first place when I'm just loading an image directly.
joshcomley
You should talk to the browser vendor. It's the browser that's making the request.
Haacked
Actually theres an IgnoreRoute.
Daniel A. White
+1  A: 

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.

+2  A: 

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.

TombMedia
A: 

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

Colin Desmond
A: 

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)
Simon_Weaver
A: 

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

Denis Volovik