Search code examples
asp.net-mvcnamespacesasp.net-mvc-routingasp.net-mvc-areasmvcroutehandler

How to specify namespace in custom route handler for MVC3?


I have written a custom route handler. Because I have Areas in my web site with conflicting controller names, I am getting the error: Multiple types were found that match the controller named...

I think I need to specify a namespace in my handler, right?

I have tried the following, none of which work:

public class MyRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {

        (... complicated DB lookups and "re-writing" of requestContext.RouteData.Values["controller"] ...)

        // doesn't work
        requestContext.RouteData.Values["namespaces"] = new[] { "Site.Contollers" };

        // doesn't work
        requestContext.RouteData.DataTokens.Add("namespaces", new[] { "Site.Contollers" });

        // doesn't work
        requestContext.RouteData.Values["namespaces"] = "Site.Contollers";

        // doesn't work
        requestContext.RouteData.DataTokens.Add("namespaces", "Site.Contollers");

        (... snip ...)

        return base.GetHttpHandler(requestContext);
    }
}

What is the correct way?

NOTE: Because my handler does database lookups and chooses different controllers based on the results, I cannot use the basic MapRoute() method in my Global.asax.cs file (as far as I know).


Solution

  • OMG, I'm dumb.

    I misspelled "Controllers." The correct answer is:

    requestContext.RouteData.DataTokens.Add("namespaces", new string[] { "Site.Controllers" });