Controller ambigous error, upgraded to MVC 2

女生的网名这么多〃 提交于 2019-12-06 09:57:24

问题


I upgraded to MVC 2, updated all my assemblies (did copy to local also).

I changed my routes to this:

routes.MapRoute(
                "Admin",
                "admin/{controller}/{action}/{id}",
                new { controller = "Admin", action = "index", id = ""},
                new[] { "MyNamespace.Web.Controllers.Admin" } // namespace
            );


 routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" },  // Parameter defaults
                new[] { "MyNamespace.Web.Controllers" } // namespace
            );

My controllers look like:

/controllers/admin/ProductController.cs
/controllers/ProductController.cs

I am still getting the error:

he controller name 'Product' is ambiguous between the following types:
MyNamespace.Web.Controllers.Admin.ProductController
MyNamespace.Web.Controllers.ProductController

Should adding the namespace fix this issue?


回答1:


Your first route suggests that there is a class /controllers/Admin/AdminController.cs. Is this correct?

Also, have a read of this. It looks like you've provided the namespace area, but they don't reside the same structure that seems to be required for ASP.NET MVC v2.

Your project solution structure should look like this:

  • Areas
    • Admin
      • ProductController
  • Controllers
    • ProductController

Your structure appears to look like this.

  • Controllers
    • Admin
      • ProductController
    • ProductController



回答2:


There was a change made to MVC 2 Beta where specifying a namespace (like "MyNamespace.Web.Controllers") would search in that namespace and its child namespaces. This differs from the MVC 1 behavior, where specifying a namespace would cause the factory to look in just that namespace.

This change will be reverted before MVC 2 RTM. Specifically, the MVC 2 RTM behavior will be that specifying "MyNamespace.Web.Controllers" will search just that namespace - just as in MVC 1 - and specifying "MyNamespace.Web.Controllers.*" (note the dot-star) will search that namespace plus its children.



来源:https://stackoverflow.com/questions/2156080/controller-ambigous-error-upgraded-to-mvc-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!