1

I'm trying to implement some domain name logic in my existing MVC5 app. The problem I'm running in to is if I try to use my custom subclass from Route, it doesn't respect the Namespaces field and throws an error because I have 2 different User controllers.

As a control, this works perfectly fine:

routes.MapRoute("Login", 
                "login/", 
                new { controller = "User", action = "Login" }, 
                new[] { "Quotes.Web.Controllers" });

My DomainRoute class inherits from Route and just adds a Domain property. Here is the relevant constructor:

public DomainRoute(string domain, string url, object defaults, string[] namespaces = null)
        : base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
    {
        Domain = domain;
        DataTokens = new RouteValueDictionary {["Namespaces"] = namespaces};
    }

and I register it like:

var loginRoute = new DomainRoute(
   domain,
   "login/",
   new { controller = "User", action = "Login" },
   new[] { "Quotes.Web.Controllers" });
routes.Add("Login", loginRoute);

DataTokens looks identical between the working version and my broken version yet it seems to ignore the fact that my DomainRoute has a Namespace entry

Multiple types were found that match the controller named 'User'. This can happen if the route that services this request ('login/') 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.

What am I missing?

AndyMcKenna
  • 2,607
  • 3
  • 26
  • 35

2 Answers2

1

I think,this will help you, i had the same issue, solved this by adding the below code

   var dataTokens = new RouteValueDictionary();
        var ns = new string[] {"MyProject.Controllers"};
        dataTokens["Namespaces"] = ns;


        routes.Add("Default", new CultureRoute(
                                  "{controller}/{action}/{id}",
                                  new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                                  null /*constraints*/,
                                  dataTokens
                                  ));
Community
  • 1
  • 1
SoftwareNerd
  • 1,875
  • 8
  • 29
  • 58
0

I switched my DomainRoute class with the much improved version found here: https://gist.github.com/IDisposable/77f11c6f7693f9d181bb

Now my route creation is just:

var clientRoutes = new DomainRouteCollection("mydomain", 
                                             "Quotes.Web.Controllers",
                                             routes);
clientRoutes.MapRoute("Login", "login/", new { controller = "User", action = "Login" });

...which is more concise and even more importantly, it works.

AndyMcKenna
  • 2,607
  • 3
  • 26
  • 35