Thursday, August 19, 2010

How To Handle The Following Error When Using StructureMap With ASP.NET MVC -The controller for path was not found or does not implement IController.

{"The controller for path 'main_bg.gif' was not found or does not implement IController."} Doesn't that error look great? If you say yes then I know that you don't have enough to do. When you are working with StructureMap, or really any dependency injector, to handle the creation of your controllers, you might run into a situation where you get the following error :

{"The controller for path 'main_bg.gif' was not found or does not implement IController."}

Of course, /Content/images/main_bg.gif won't be in YOUR error message, but some image you are using will likely be in your message. As you can see, StructureMap seems to be looking for a controller for the path of one of my image files. Well isn't that great? Now what? Fear not my friends, the solution to this problem is very, very simple - I promise. Before I tell you the answer, I'm going to ask you a question. What area can you use in MVC to tell your program to ignore or follow certain paths? .........1..........2.........3........Give up? It is the RegisterRoutes area of the global.asax. In this case, you just need to tell your code to ignore the route to your beautiful graphic and StructureMap will stop giving you the error. See, I told you it was simple!

Smooches,
Kila

Problem:
You are receiving the following error when you are debugging your ASP.NET MVC application in Visual Studio.
{"The controller for path 'main_bg.gif' was not found or does not implement IController."}
(You might also see something like the following:
{"The controller for path '/' was not found or does not implement IController."})

Solution:
Change your global.asax RegisterRoutes routine to ignore the offending path.

Example: routes.IgnoreRoute("{*ignoregifs}", new { ignoregifs = @".*\.gif(/.*)?" }); This will ignore all gifs.

If your issue is some other type of file or "/", you simply adjust the regular expression accordingly.