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).

有帮助吗?

解决方案

OMG, I'm dumb.

I misspelled "Controllers." The correct answer is:

requestContext.RouteData.DataTokens.Add("namespaces", new string[] { "Site.Controllers" });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top