Sometimes we encounter two items together, then inevitably encounter two controllers of the same name, in fact, MVC in the registration of routes, add route when you can specify the current rule to resolve all controllers under that namespace.
Note: The controller's call is through Icontrollerfactory, reflection calls the target controller, does not specify the target namespace, directly from Buildmanager.getreferencedassemblies (); As follows.
Private StaticList<type> controllertypes =NewList<type>(); /// <summary> ///Static Constructors/// </summary> Staticdefaultcontrollerfactory () {varAssemblys =buildmanager.getreferencedassemblies (); foreach(Assembly AssemblyinchAssemblys) { varTypes = assembly. GetTypes (). Where (A =typeof(IController). IsAssignableFrom (a)); foreach(Type typeinchtypes) {Controllertypes.add (type); } } } /// <summary> ///Create a controller/// </summary> /// <param name= "RequestContext" ></param> /// <param name= "Controllername" ></param> /// <returns></returns> PublicIController Createcontroller (RequestContext RequestContext,stringcontrollername) { stringTypeName = Controllername +"Controller"; Type Controllertype= Controllertypes.firstordefault (A = A.name = =)typeName); if(Controllertype! =NULL) { return(IController) activator.createinstance (Controllertype); } return NULL; }
Back to the chase: How to support multiple namespaces
Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{controller}/{action}", defaults:New{controller ="Home", action ="Index"}, namespaces:New string[] {"mvcexpose.controllers" } ); Routes. MapRoute ("Admin","Admin/{controller}/{action}", New{controller ="Home", action ="Index"},New string[] {"MvcExpose.Admin.Controllers" }); }
Remember: Two registration URL rules, to the length of inconsistent, default with "{Controller}/{action}/{id}" has been an error, that in the regular resolution, it is impossible to distinguish between the two routing rules, so when "http://localhost : 4500/admin/home/index ", the system first calls the default route resolution.
MVC supports routing with the same name, different namespaces