The following should work till they have the GetControllerType of the MvcHandler class take namespaces into account, at which time I suggest you forget these use theirs.
public class MvcRouteHandlerNS : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MvcHandlerNS()
{
RequestContext = requestContext
};
}
}
public class MvcHandlerNS: MvcHandler
{
protected static object _customLock = new object();
protected static Dictionary<string, Type> _typeCache = new Dictionary<string, Type>();
protected override Type GetControllerType(string controller)
{
IDictionary<string,object> routeDefaults = this.RequestContext.RouteData.Values;
if (routeDefaults.Keys.Contains("ns"))
{
string typeName = String.Format("{0}.{1}Controller", routeDefaults["ns"], controller);
Type controllerType = null;
lock (_customLock)
{
if (_typeCache.ContainsKey(typeName))
controllerType = _typeCache[typeName];
if(controllerType == null)
{
controllerType = Type.GetType(typeName);
if (controllerType!=null)
{
_typeCache.Add(typeName, controllerType);
}
}
}
if(controllerType!=null)
return controllerType;
}
return base.GetControllerType(controller);
}
}
And in the glabal.asax class use
RouteTable.Routes.Add(new Route
{
Url = "SomeDirectory/[action]/[id]",//Base path needs to be specified
Defaults = new { controller = "Bar", ns = "Company.MySite.Controllers.Foo", action = "Index", id = (string)null },
RouteHandler = typeof(MvcRouteHandlerNS)
});
Though personally, I would just name my somedirectory/foobarcontroller SomeDirectoryFooBarController and SomeOtherDirectoryFooBarController, and forget I ever saw this code. At least until they add support for namespaces in the controllers.
Edit: Especially since it looks like they are looking into it already.