I have a solution in Visual Studio 2015 with multiple MVC projects, based on areas, working correctly and redirecting from Test1, which is the main project, but I need to use subdomains to obtain information depending on it. My project is like the following image:
Now to capture the subdomains I did a test with a project without areas with the following code that worked correctly for me:
SubdomainRoute
namespace Prueba1.App_Start
{
public class SubdomainRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
if (httpContext.Request == null || httpContext.Request.Url == null)
{
return null;
}
var host = httpContext.Request.Url.Host;
var index = host.IndexOf(".");
string[] segments = httpContext.Request.Url.PathAndQuery.TrimStart('/').Split('/');
if (index < 0)
{
return null;
}
var subdomain = host.Substring(0, index);
string[] blacklist = { "www", "admix", "mail" };
if (blacklist.Contains(subdomain))
{
return null;
}
string controller = "Home";
string action = "Index";
var routeData = new RouteData(this, new MvcRouteHandler());
routeData.Values.Add("controller", controller); //Goes to the relevant Controller class
routeData.Values.Add("action", action); //Goes to the relevant action method on the specified Controller
routeData.Values.Add("id", ""); //pass subdomain as argument to action method
return routeData;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return null;
}
}
}
RouteConfig
namespace Prueba1
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new SubdomainRoute());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
But when trying it with my new project structure based on areas it gives me this error:
Error de servidor en la aplicación '/'.
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
Proyecto2.Controllers.HomeController
Proyecto1.Controllers.HomeController
Prueba1.Controllers.HomeController
Descripción: Excepción no controlada al ejecutar la solicitud Web actual. Revise el seguimiento de la pila para obtener más información acerca del error y dónde se originó en el código.
Detalles de la excepción: System.InvalidOperationException: Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
Proyecto2.Controllers.HomeController
Proyecto1.Controllers.HomeController
Prueba1.Controllers.HomeController
Error de código fuente:
Se ha generado una excepción no controlada durante la ejecución de la solicitud Web actual. La información sobre el origen y la ubicación de la excepción pueden identificarse utilizando la excepción del seguimiento de la pila siguiente.
Seguimiento de la pila:
[InvalidOperationException: Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
Proyecto2.Controllers.HomeController
Proyecto1.Controllers.HomeController
Prueba1.Controllers.HomeController]
System.Web.Mvc.DefaultControllerFactory.GetControllerTypeWithinNamespaces(RouteBase route, String controllerName, HashSet`1 namespaces) +256
System.Web.Mvc.DefaultControllerFactory.GetControllerType(RequestContext requestContext, String controllerName) +651
System.Web.Mvc.DefaultControllerFactory.System.Web.Mvc.IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, String controllerName) +57
System.Web.Mvc.MvcRouteHandler.GetSessionStateBehavior(RequestContext requestContext) +208
System.Web.Mvc.MvcRouteHandler.GetHttpHandler(RequestContext requestContext) +45
System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +12040727
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +142
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +92
To give you a better idea of the structure of my project, you can see the following link .
I know that the reference to the controllers of my main application is missing, but I don't know how to put them to avoid the error.
The solution was simple, just change the name of the controller , since the error marked said that it could not find which controller should be accessed since there were 3 controllers with the same name.
Once you change the controller name change it in the SubdomainRoute file leaving it like this: