ASP.NET MVC2 Routing Issue With StructureMap
        Posted  
        
            by alphadogg
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by alphadogg
        
        
        
        Published on 2010-05-10T03:32:36Z
        Indexed on 
            2010/05/10
            3:38 UTC
        
        
        Read the original article
        Hit count: 966
        
So, in global.asax, I have:
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute(" { *favicon }", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
    routes.IgnoreRoute("{*robotstxt}", new { robotstxt = @"(.*/)?robots.txt(/.*)?" });
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}
protected void Application_Start()
{
    var container = new Container();
    ServiceLocator.SetLocatorProvider(() => new StructureMapServiceLocator(container));
    ComponentRegistrar.Register(container);
    Debug.WriteLine(container.WhatDoIHave());
    // original MVC2 code at project startup
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
}
and in my StructureMapControllerFactory, I have:
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
    if (requestContext == null)
    {
        throw new ArgumentNullException("requestContext");
    }
    if (controllerType == null)
    {
        //return base.GetControllerInstance(requestContext, controllerType);
        controllerType = GetControllerType(requestContext, "Home");
        requestContext.RouteData.Values["Controller"] = "Home";
        requestContext.RouteData.Values["action"] = "Index";
    }
    try
    {
        return theContainer.GetInstance(controllerType) as Controller;
    }
    catch (StructureMapException)
    {
        System.Diagnostics.Debug.WriteLine(theContainer.WhatDoIHave());
        throw;
    }
}
Now, normally, in most examples you find on the net, when controllerType is null, you have the commented out line (base.GetControllerInstance()) that handles it. However, if I use that, I get an error about not finding the controller for default.aspx when the controllerType is null at startup of the web app. If I force the use of the Home controller, I get the site to appear.
What am I doing wrong? Is it a problem in routing?
© Stack Overflow or respective owner