解决 Google Chrome 读取 favicon.ico 错误

今天在使用 ASP.NET MVC 3 时遇到一个小问题,由于在 Google Chrome 中浏览网页时会自动请求 favicon.ico ,所以当没有为网站添加 favicon.ico 时会出现将"favicon.ico" 作为参数传递。
我们可以在 ASP.NET MVC 3 路由注册方法 RegisterRoutes() 中添加如下代码,这样当请求 favicon.ico 时 .net 不会做任何处理。


routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}",
    new { favicon = @"(.*/)?favicon.ico(/.*)?" }
);
可见,为了阻止返回 404 错误代码,为网站添加一个 favicon.ico 是个不错的做法,当然作为开发人员在调试时可以先使用一个替代品,如我会使用 Google 的 favicon.ico。方法为在_Layout.cshtml页面中添加:


<title>@ViewBag.Title</title>
<link rel="shortcut icon" href="http://www.google.com/favicon.ico" />
等项目正式上线时,再将它替换即可,这样就解决了 Google Chrome 中调试 ASP.NET MVC 3 项目时,由于 favicon.ico 不存在时的问题,希望对你有所帮助!

[root@iZ2ze3k6buohug6id3eg97Z dist]# ls /usr/local/java/nginx/html/dist/ assets favicon.ico index.html [root@iZ2ze3k6buohug6id3eg97Z dist]# /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2025/03/24 05:30:18 [error] 24#24: *1 rewrite or internal redirection cycle while internally redirecting to “/index.html”, client: 117.189.228.151, server: 182.92.154.131, request: “GET / HTTP/1.1”, host: “182.92.154.131” 117.189.228.151 - - [24/Mar/2025:05:30:18 +0000] “GET / HTTP/1.1” 500 579 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36” 117.189.228.151 - - [24/Mar/2025:05:30:18 +0000] “GET /favicon.ico HTTP/1.1” 500 579 “http://182.92.154.131/” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36” 2025/03/24 05:30:18 [error] 24#24: *2 rewrite or internal redirection cycle while internally redirecting to “/index.html”, client: 117.189.228.151, server: 182.92.154.131, request: “GET /favicon.ico HTTP/1.1”, host: “182.92.154.131”, referrer: "http://182.92.154.131/"分析日志看看问题解决了m
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值