Showing posts with label StructureMap. Show all posts
Showing posts with label StructureMap. Show all posts

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.

Monday, August 9, 2010

Use StructureMap To Create And Dispose Of NHibernate Sessions

If you are using StructureMap to manage sessions for NHibernate, you may have encountered situations where you would like to dispose of those sessions in a clean and efficient manner. You could create your own code to manage this (good luck with that) or you could use StructureMap to handle that too.

In the current version of StructureMap, you can use the following code placed in the Application_EndRequest event of your Global.asax file.

ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();

If you are using an old release of StructureMap, you can use the following code placed in the Application_EndRequest event of your Global.asax file.

HttpContextBuildPolicy.DisposeAndClearAll();

Either way, you can use StructureMap to manage the clean up of your sessions in NHibernate. I hope this knowledge makes your life easier!


Smooches,

Kila

Sunday, August 8, 2010

ASP.NET MVC 2 & StructureMap Error - ASP.NET MVC 2 - GetControllerInstance(System.Type)': no suitable method found to override - OH NO!

Don't you just wish that when you port your applications from ASP.NET MVC 1 to ASP.NET MVC 2 and beyond that things just worked? If it did, I guess it would put some of us out of work. It is always nice to know that thanks to these updates and the technologies we employ to make our lives easier, we will always have jobs.

StructureMap and ASP.NET MVC 2 gave me a nice little error that you and others might run across. The solution is quick and easy - unless you don't know it (LOL). While moving one of my applications from MVC 1 to MVC 2, I came across the StructureMap error. Here is the offending StructureMap code I used in ASP.NET MVC 1.

CODE THAT DOES NOT WORK IN ASP.NET MVC 2
protected override IController GetControllerInstance(Type controllerType)
{
if (controllerType == null) return null;
try
{
return (IController)ObjectFactory.GetInstance(controllerType);
}
catch (StructureMapException)
{
System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw;
}
}

It looks like it should work right? Well it did work, but guess what? Times have changed....well StructureMap has changed. So now the code should look like the following:

CODE THAT WILL WORK IN ASP.NET MVC 2
protected override IController GetControllerInstance(RequestContext requestContext,
Type controllerType)
{

if (controllerType == null)
return base.GetControllerInstance(requestContext, controllerType);

try
{
return (IController)ObjectFactory.GetInstance(controllerType);
}
catch (StructureMapException)
{
System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw;
}
}
And there you have it! Changes, changes, changes! I hope this puts a smile on your face or at least lets you get back to work.

Smooches,
Kila