I have a bit of a problem. I have an area called Framed. This area has a home controller. The default for the site also has a home controller.
What I'm trying to do with this is have a version of each controller/action that is suitable for an IFrame, and a version that is the normal site. I do this through Master pages, and the site masterpage has many different content place holders than the framed version. For this reason I can't just swap the master page in and out. For example, http://example.com/Framed/Account/Index will show a very basic version with just your account info for use in an external site. http://example.com/Account/Index will show the same data, but inside the default site.
My IoC container is structuremap. So, I found http://odetocode.com/Blogs/scott/archive/2009/10/19/mvc-2-areas-and-containers.aspx and http://odetocode.com/Blogs/scott/archive/2009/10/13/asp-net-mvc2-preview-2-areas-and-routes.aspx. Here's my current setup.
Structuremap Init
ObjectFactory.Initialize(x =>
{
x.AddRegistry(new ApplicationRegistry());
x.Scan(s =>
{
s.AssembliesFromPath(HttpRuntime.BinDirectory);
s.AddAllTypesOf<IController>()
.NameBy(type => type.Namespace + "." + type.Name.Replace("Controller", ""));
});
});
The problem here that I found through debugging is that because the controllers have the same name (HomeController), it only registers the first one, which is the default home controller. I got creative and appended the namespace so that it would register all of my controllers.
Default Route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new[] { "MySite.Controllers" }
);
Area route
context.MapRoute(
"Framed_default",
"Framed/{controller}/{action}/{id}",
new { area = "Framed", controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "MySite.Areas.Framed.Controllers" }
);
As recommended by Phil Haack, I am using the namespaces as the 4th parameter
app start, just to prove the order of initialization
protected void Application_Start()
{
InitializeControllerFactory();
AreaRegistration.RegisterAllAreas();
RouteConfiguration.RegisterRoutes();
}
Controller Factory
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
IController result = null;
if (controllerType != null)
{
result = ObjectFactory.GetInstance(controllerType)
as IController;
}
return result;
}
So, when I hit /Home/Index, it passes in the correct controller type. When I hit /Framed/Home/Index, controllerType is null, which errors because no controller is returned.
It's as if MVC is ignoring my area altogether. What's going on here? What am I doing wrong?