ASP NET MVC Area routing/multiple routes issue in VB
I'm pretty inexperienced with .net and have just started learning MVC. I've hit an issue concerning multiple controllers being found:
"Multiple types were found that match the controller named 'reviews'. This can happen if the route that services this request ('{controller}/{action}/{id}') 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."
I've recently added a new "Admin" area to my app and within that I have a "ReviewController". There is also a "ReviewController" within the main app folder:
ah - as a new user I can't post an image, but basically I have a "ReviewController" in "Controllers" and in "Areas/Admin/Contollers".
I have 2 routes set up so far:
Default route in Global.asax.vb
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
' MapRoute takes the following parameters, in order:
' (1) Route name
' (2) URL with parameters
' (3) Parameter defaults
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, _
{"PowellCasting/Controllers"}
)
End Sub
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
System.Data.Entity.Database.SetInitializer(New System.Data.Entity.DropCreateDatabaseIfModelChanges(Of Models.PowellCastingEntites))
Database.SetInitializer(Of PowellCastingEntites)(New PowellCastingInitializer())
RegisterGlobalFilters(GlobalFilters.Filters)
RegisterRoutes(RouteTable.Routes)
ControllerBuilder.Current.DefaultNamespaces.Add("PowellCasting/Controllers")
End Sub
Area route in AdminAreaRegistration
Namespace PowellCasting.Areas.Admin
Public Class AdminAreaRegistration
Inherits AreaRegistration
Public Overrides ReadOnly Property AreaName() As String
Get
Return "Admin"
End Get
End Property
Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
context.MapRoute( _
"Admin_default", _
"Admin/{controller}/{action}/{id}", _
New With {.Controller = "Dashboard", .action = "Index", .id = UrlParameter.Optional}
)
End Sub
End Class
End Namespace
After reading around the issues I was having, I have added a number of bits of code:
My Admin controllers have the right namespace defined
- Namespace PowellCasting.Areas.Admin rather than simply PowellCasting.
- I have RegisterAllAreas set in the global
- ControllerBuilder.Current.DefaultNamespaces.Add("PowellCasting/Controllers") is in place to specify the default route.
The specific problem I have now is that when I go to "/Reviews" I get the multiple controllers error shown above, specifically:
*The request for 'reviews' has found the following matching controllers: PowellCasting.PowellCasting.Areas.Admin.ReviewsController
PowellCasting.PowellCasting.ReviewsController*
I've enabled the route debugger and that only shows one match:
ah - as a new user I can't post an image but it shows:
Admin/{controller}/{action}/{id} as FALSE
and
{controller}/{action}/{id} as TRUE
This is as expected so I don't know why I'm receiving the issue.
I have read about overloading the maproute method with the namespace, but couldn't find an example in VB (loads in c#). But I tried this:
Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
context.MapRoute( _
"Admin_default", _
"Admin/{controller}/{action}/{id}", _
New With {.Controller = "Dashboard", .action = "Index", .id = UrlParameter.Optional}, _
vbNull,
{"PowellCasting/Areas/Admin/Controllers"}
)
End Sub
and
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/开发者_如何转开发{*pathInfo}")
' MapRoute takes the following parameters, in order:
' (1) Route name
' (2) URL with parameters
' (3) Parameter defaults
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, _
vbNull,
{"PowellCasting/Controllers"}
)
End Sub
but without success.
I'm sure this should be straightforward and I've tried a number of things - it very frustrating. Any help would be really appreciated.
My first post on here - Hi! :)
If you read carefully the error message you are getting:
The request for 'reviews' has found the following matching controllers: PowellCasting.PowellCasting.Areas.Admin.ReviewsController PowellCasting.PowellCasting.ReviewsController
you will notice that PowellCasting
is repeated twice.
So in your main Global.asax route registration:
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, _
vbNull,
{"PowellCasting.Controllers"}
)
this assumes that your ReviewController
in the root is defined like this:
Namespace Controllers
Public Class ReviewController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Return View()
End Function
End Class
End Namespace
Notice the absence of the PowellCasting
prefix in the namespace definition (that's a VB subtility which adds it automatically, I suppose that's the name of your application)
and inside the AdminAreaRegistration.vb
:
context.MapRoute( _
"Admin_default", _
"Admin/{controller}/{action}/{id}", _
New With {.action = "Index", .id = UrlParameter.Optional}, _
vbNull, _
{"PowellCasting.Areas.Admin.Controllers"}
)
which assumes that your ReviewController
in this area is defined like so
Namespace Areas.Admin.Controllers
Public Class ReviewController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Return View()
End Function
End Class
End Namespace
Once again notice the absence of the PowellCasting
prefix in the namespace definition.
I too had a similar problem.
I had an application where i had two areas : Common , Products
And when i opened the application , the Common_default route was being called, but when i saw the RoutesCollection.Routes, it showed me 4 routes. Actually i defined only 2 routes - one for common , one for Products.
After reading the above comments, i just gave it a try my changing the project properties for my website.
I changed the default namespace from "MyWebsite.Web" to "MyWebsite" . And it did the trick. My applciation was up and running.
The bottomline is never have "." in the default namespace for your MVC4 website project.
DealerNetworksAreaRegistration.cs : //Add Area
context.MapRoute(
"DealerNetworks_default",
"DealerNetworks/{controller}/{action}/{id}",
new { controller = "Dealer", action = "Index", id = UrlParameter.Optional }
);
RouteConfig.cs
//Add Namespace
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional },
//passing the namespace of the HomeController in the Main area using namespace parameter.
namespaces: new[] { "SMART.Controllers" }
);
Global.asax.cs
//Register Areas
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
精彩评论