Search code examples
razorasp.net-mvc-4area

Cannot get rid of "Multiple types were found that match the controller"


I have tried out every possible link to this subject in StackOverflow, all suggestions given here, viewed the video by Rick Anderson but without any positive result. Still getting the dreadfull "multiple types were found....'" message. And my project is just a setup to try the Area phenomena. Is the use of Area's at all possible with Razor viewer? Or only with ASPX??

This is what I have: In Global.asax:

   routes.MapRoute( _
        "Default", _
        "{controller}/{action}/{id}", _
        New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, New String() {"MyApplication.Controllers"})

In three separate AreaRegistration files:

        Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
        context.MapRoute( _
            "Shopping_default", _
           "Shopping/{controller}/{action}/{id}", _
            New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional})
    End Sub

and:

        Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
        context.MapRoute( _
            "Blog_default", _
           "Blog/{controller}/{action}/{id}", _
            New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional})
    End Sub

and:

        Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
        context.MapRoute( _
            "Admin_default", _
           "Admin/{controller}/{action}/{id}", _
            New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional})
    End Sub

I have had the three AreaRegistration files WITHOUT the extra reference to ".controller = "Home" ", but it all gives the same results. I have also tried to add the extra parameter for the namespace to these three declarations. I have tried to add this to the Global.asax:

        ControllerBuilder.Current.DefaultNamespaces.Add("MyApplication.Admin.Controllers")
    ControllerBuilder.Current.DefaultNamespaces.Add("MyApplication.Blog.Controllers")
    ControllerBuilder.Current.DefaultNamespaces.Add("MyApplication.Shopping.Controllers")

    ControllerBuilder.Current.DefaultNamespaces.Add("MyApplication.Controllers")

but that also with NO positive outcome. I have tried without the application name in the AreaRegistration classes also. What possible other actions ca I try (and with successful outcomes)? The projectstrcuture is straightforward: I started with an empty project, immediately added the three areas, the HomeControllers and the Index views. No changes to namespaces, all left as default as possible. The MVC 4 template was used. Any suggestions by anyone?

Peter


Solution

  • The issue was solved like so:

    1. Alter the default Namespaces that were given by the template, for every controller, by adding the %AppName%.Areas.%AreaName%.Controllers. So in the example above, the HomeController for the Admin area was put in the Namespace

      Namespace MyApplication.Areas.Admin.Controllers
      

      This happens to be the same Namespace as there is in the AreaRegistration class that was rendered by the template, with the "Controllers" suffix added to it.

    2. Alter the code in Global.asax like so:

      routes.MapRoute( _
          "Default", _
          "{controller}/{action}/{id}", _
          New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, New String() {"MyApplication"} _
      )
      

      So here the "MyApplication" Namespace is added. This works correct UNDER THE PRESUMPTION that there is NO NAMESPACE added in the application root HomeController (this is the default). If there is a namespace in the root HomeController, that should be used in the Global.asax file.

    I have found out that there are other ways to solve this same issue, but this is the one with the LEAST code changes compared to the template-generated code.