当我从 ASP.NET MVC 提供文件时,为什么 Chrome 会搜索我的 favicon.ico?

发布于 2024-07-24 22:40:51 字数 1349 浏览 6 评论 0原文

我在 MVC 中有一个控制器,提供数据库中的图像。

编辑:如果我在 MVC 中通过完全标准的方式提供文件,这种情况仍然会发生。

每次我请求图像时,Google Chrome 也会搜索我的 favicon.ico。

为了避免对“我也应该关心”的其他​​事情进行不必要的讨论,让我们假设我不关心本示例中的任何缓存,并且我将始终随文件返回 HTTP 响应 200。

在我的控制器中,我返回以下内容:

return File(fileBytes, contentType);

检查 Fiddler 2 后,生成以下响应:

HTTP/1.1 200 正常
缓存控制:公共
内容类型:图像/gif
ETag:oYu19wKo+KEHkyxZQ2WXAA==
服务器:Microsoft-IIS/7.0
X-AspNetMvc-版本:1.0
X-AspNet-版本:2.0.50727
X-Powered-By:ASP.NET
日期:2009 年 6 月 16 日星期二 18:48:45 GMT
内容长度:29344

相比之下,当我(第一次)请求 Google 徽标时,这是 Google 在 Fiddler 中的响应:

HTTP/1.1 200 正常
内容类型:图像/gif
最后修改时间:2006 年 6 月 7 日,星期三 19:42:34 GMT
日期:2009 年 6 月 16 日星期二 18:50:54 GMT
到期时间:2010 年 6 月 16 日星期三 18:50:54 GMT
缓存控制:公共,最大年龄=31536000
服务器:gws
内容长度:8706
年龄:2

但是,在 Chrome 中获取我的图像后,Chrome 会尝试找到我的 favicon.ico。 在请求 Google 徽标后,它不会尝试此操作。

有什么想法为什么会发生这种情况吗? 根据我对 HTML 的理解,答案必须在响应标头中,因为这肯定是客户端必须继续进行的全部操作吗? 请纠正我!

编辑2:似乎很多人完全误解了这个问题。 问题不是缺少 favicon 以及 MVC 中的错误请求 - 这是在仅加载内容类型为“IMAGE/JPEG”的图像时请求 favicon 的问题”,而不是内容类型为“TEXT/HTML”的网页!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

○愚か者の日 2024-07-31 22:40:51

这与MVC无关。 我正在使用带有自定义构建日志服务的网络表单,我偶然发现了这篇文章,想知道为什么我的日志中不断出现“文件不存在”错误。 这是在我的开发机器本地,我的项目中没有 favicon.ico 文件,我尝试过 IE、Firefox 和 Google 试图看看哪个浏览器是罪魁祸首。

Google Chrome 向我的应用发出的每个请求都会请求 favicon.ico。 我必须开始在本地登录浏览器以确定罪魁祸首实际上是谷歌浏览器。 如果它打扰你,我会联系谷歌。 我只是想确保它不是某种新的木马感染了我的 chrome。

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.

小…楫夜泊 2024-07-31 22:40:51

实际答案:这是一个已知的、经过验证的错误。 *(最近已修复!...也许?)

看起来像是 Chrome 的一个已知的长期存在的问题:
http://crbug.com/39402

如果您希望尽快修复该问题,请为该问题加注星标。 更多人关注该问题可能会提高其优先级,并可能更快地得到解决。


****更新 1***:截至今年(2013 年)5 月 15 日(此问题提出四年后),该问题似乎已在版本 29 中得到修复:
http://crbug.com/39402#c47

随意撤消所有的黑客行为和解决方法。 :]

****更新 2 (2015-01)***:根据同一问题链接,这对于某些用户来说显然仍然是一个问题。 :/

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. :/

岛徒 2024-07-31 22:40:51

你有网站图标吗? 如果没有,也许这就是 Chrome 每次都尝试为您的网站查找它的原因。 对于谷歌来说,它已经缓存了图标。

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.

糖粟与秋泊 2024-07-31 22:40:51

您可以做的一件事是让 MVC 忽略对 *.ico 的任何请求,这样您在调试时就不会遇到任何异常。

应该是这样的:

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

该 URL 模式匹配所有内容,但我们将其限制为仅匹配以 favicon.ico 结尾的任何内容。 (我没有测试过这个)

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)

奈何桥上唱咆哮 2024-07-31 22:40:51

我不久前遇到了这个问题,并通过添加

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

到 Global.asax 中的 RegisterRoutes 方法来忽略特定路由来解决这个问题。

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.

伴我心暖 2024-07-31 22:40:51

在我看来,Chrome 为自己的选项卡请求了一个 favicon - 我不断收到 404 错误(因为我的 favicon 在其他地方并且我的页面知道它),直到我做了一些测试并意识到是 Chrome 直接向 favicon 文件发出请求。 我猜除了重写真实文件之外没有真正的修复

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

纵情客 2024-07-31 22:40:51

您可以在 web.config 文件中添加类似的内容,以确保 favicon.ico 缓存在客户端上,并且不会每次都被请求。

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

您可以/应该对任何图像/.js 和 css 文件执行相同的操作

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

葵雨 2024-07-31 22:40:51

您应该设置 Expires 标头来告诉浏览器应该使用其本地副本多长时间。

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

白鸥掠海 2024-07-31 22:40:51

如果您检查项目设置,它会在某处显示默认图标。 删除那个?

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

也只是曾经 2024-07-31 22:40:51

Chrome 浏览器可以以不同于任何其他网站的方式与 Google 网站配合使用,因此,首先,我建议每次在其他地方(例如在 StackOverflow 上)检查它是否查找 favicon.ico。

我还会检查 Firefox 是否对您的网站执行同样的操作。 我认为每个浏览器运行时只应请求 favicon.ico 一次,即使它不存在于网站上。 这可能是您使用的 Chrome 版本中的错误。

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.

顾冷 2024-07-31 22:40:51

这个 SO问题/答案解释了如何向浏览器提供Favicon通过使用路线。

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

泪眸﹌ 2024-07-31 22:40:51

将 ICON 链接放入母版页中非常重要,否则某些浏览器会尝试为所有目录查找 favicon.ico,而不仅仅是全局查找一次。

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

从我的日志来看,谷歌工具栏似乎是罪魁祸首(当然还有IE6)。 他们都会请求根目录以外的目录

 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文