X

ASP.NET MVC: Ignore requests to favicon.ico

One problem I faced when writing my example pet portal on ASP.NET MVC was Google Chrome’s request to favicon.ico that doesn’t exist. IE8 that I use (8.0.7000.0) doesn’t make these blind requests to discover that favicon.ico is not there. Solution to my problem was very simple.

Just add the ignore rule for favicon.ico to Global.asax file, in the beginning of RegisterRoutes method:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

    // Other route definitions follow here
}

The rule for favicon works for all folders in your application. There is also one other interesting thing. If you ignore some route then ASP.NET MVC lets those requests to files directly to web server. It doesn’t direct them to controllers.

Liked this post? Empower your friends by sharing it!
Categories: ASP.NET

View Comments (8)

  • I was adding some error logging code to my application last weekend when I kept getting the "File not found" error for favicon.ico. In the end i just created the damn thing to shut it up. But I prefer your solution, thanks!

  • Thank you for the solution, I'm going to add a global error handler for my current MVC project and favicon.ico request is very annoying.

  • Perfect! This will really help, as I'm using an error handler to email me site errors and continually get emails regarding favicon.ico.

  • Sorry to say but above solution doesn't work. I cleared the history before running application in chrome.

  • Not sure what you mean solution doesn't work. It doesn't make browsers to stop asking for favicon. It just doesn't let these requests through MVC pipeline if favicon is missing.

Related Post