views:

891

answers:

3

I'm still trying to figure things out with StructureMap and one of the issues i'm running into is my Controller Factory class blowing up when a null controller type is passed to it. This only happens when the application builds for the first time, after which every subsequent build works fine. Even when i shutdown Visual Studio and reopen the project (I'm not running this in IIS). It's almost like there is some sort of caching going on. This is what the controller class looks like:

public class IocControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(Type controllerType)
    {
        try
        {
            return (Controller)ObjectFactory.GetInstance(controllerType);
        }
        catch (StructureMapException)
        {
            System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
            throw;
        }
    }
}

What could be wrong? Do i need to have every controller registered? Thank you.

+4  A: 

I ran into the same problem with a controller factory built around ninject.

It seems MVC will pass you null for controllertype when it can't resolve a route from the routing table or when a route specifies a none existing controller. I did two things to solve this. You might want to check your route table and add a catchall route that shows a 404 error page like described here http://stackoverflow.com/questions/318886/net-mvc-routing-catchall-not-working

You could also check with the routing debugger what goes wrong. http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Mendelt
Another solution, when extending an existing controller factory: Check if controllerType is null, if so let the existing factory handle the request instead of handling it yourself.
Frank Schwieterman
+2  A: 

I was having the similar problem. I believe it was HTTP requests for nonexistent images, CSS files, etc.

We know the MVC routing first looks to see if the requested file physically exists. If it doesn't then the URL gets tested against the configured Routes. I think the request for an image that didn't physically exist was passed to the Routing engine, and didn't match any routes, so NULL was used.

So to fix it, use FireBug or something to watch for, and fix, broken HTTP requests. During development, I used a route like this to temporarily bypass these issues (all of my resource folders start with an underscore like _Images, _Styles, etc):

routes.IgnoreRoute("_*");  // TODO: Remove before launch

Hope this helps!

Jay Querido
+5  A: 

Most browser are looking for a favicon.ico when you load a site, and there is probably some caching involved with this behavior, this might explain the odd "Only fail on the first build" thing you mentionned.

In my case this was causing the problem of the null controller type in the controller factory.

Adding a routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); in global.asax makes the error go away, the request should fall through to the filesystem without MVC looking for a favico.ico controller in your code.

Here is a link to Gunnar Peipman post about this

I found out by overriding GetControllerType(string controllerName) in my custom controller factory class and checking what the controllerName value was for each request.

Joel Gauvreau
You have given the right answer for the problem ;) Thanks!
Rookian