66

I get an error:

"The controller for path '/favicon.ico' was not found or does not implement IController"

Then I thought: how does the framework know for which files it has to instantiate a controller, because the same thing is true for script, css and other files?

(never thought of that, but now the favicon is complaining, I was wondering....)

But back to the error, why does that occur?

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
Michel
  • 23,085
  • 46
  • 152
  • 242
  • 2
    possible duplicate of [Serving favicon.ico in ASP.NET MVC](http://stackoverflow.com/questions/487230/serving-favicon-ico-in-asp-net-mvc) – codingbadger Jan 07 '11 at 09:42

5 Answers5

111

Add this to you global.asax:

routes.IgnoreRoute("favicon.ico");
Steve
  • 50,173
  • 4
  • 32
  • 41
  • 4
    Thanks, that worked great. I was using ELMAH and it kept giving me an error. To make sure no error about favicon.ico appears in ELMAH, make sure to also add the file "favicon.ico" in the root folder of your site. – mateuscb Sep 22 '11 at 22:43
19

You can also specify the ignore route with constraints

routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
  • Is there any rationale behind using a complex approach when the simple `routes.IgnoreRoute("favicon.ico");` seems to be enough? Perhaps some browser tries to automatically get the favicon, and uses a funny URL? – ANeves Apr 17 '12 at 09:59
  • 3
    The simple approach would work only if favicon.ico is placed in the root of the site as @mateuscb pointed out in his comment, however if one places it somewhere else as described in http://stackoverflow.com/questions/487230/serving-favicon-ico-in-asp-net-mvc then we need this more complicated approach. – yoel halb Jul 17 '12 at 20:45
  • 1
    Anyone using this code snippet should be aware that there is a syntax error as "routes" should be lowercase since we are dealing with an instance and not with a type – yoel halb Jul 17 '12 at 20:50
  • 2
    @yohal - whoops - fixed it as per http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx – Nicholas Murray Jul 17 '12 at 21:58
15

The top answers are correct.

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

But for newer versions of MVC you must add this at the top of the RegisterRoutes method in RouteConfig.cs (so somewhere before routes.MapRoute(..) is called).

Yodacheese
  • 4,787
  • 5
  • 34
  • 42
3

You are probably getting this with the VS web server. Right?

You wouldn't get this with IIS since IIS (by default) handles requests for images (.ico, .jpg, .gif, et cetera) and therefore they don't make it to your app.

Hector Correa
  • 26,290
  • 8
  • 57
  • 73
  • my first reaction was yes, but no, it's als on iis6 + win2003 – Michel Jan 09 '11 at 16:16
  • 1
    Also on IIS 7. You must add the ignoreroute for favicon and for robots.txt as well. – James Reategui Sep 12 '11 at 17:10
  • 2
    IIS 7 has this ["integrated pipeline"](http://www.iis.net/learn/application-frameworks/building-and-running-aspnet-applications/how-to-take-advantage-of-the-iis-integrated-pipeline) thing where HTTP handlers get a crack at pretty much any URL that hits the site, IIRC. – cHao Sep 13 '12 at 14:00
0

Interesting as it sounds I got this error only if I had checked the "Enable Just My Code" option under tools->options->debugging, and as soon as I unchecked it I am no longer getting this error.

Note however that it appears that the error is still being thrown behind the scenes but being caught immediately internally, so the best solution is to code in the global.asax to ignore it as the other answers suggest.

Diego
  • 34,802
  • 21
  • 91
  • 134
yoel halb
  • 12,188
  • 3
  • 57
  • 52