ASP.NET MVC Routing Scope / Multiple Routes in VB

I am pretty inexperienced with .net and just started learning MVC. I ran into a problem with detecting multiple controllers:

“Several types were found that match the controller named“ reviews. ”This can happen if the route serving this request ('{controller} / {action} / {id}') does not define a namespace for the search that matches the request If so, register this route by overloading the MapRoute method, which accepts the namespaces parameter.

I recently added a new “admin” area to my application, and inside that I have a “ReviewController”. There is also a “ReviewController” in the main application folder:

ah - as a new user, I can’t post the image, but basically I have a “ReviewController” in the “Controllers” and in the “Scopes / Admin / Controllers”.

I have 2 routes configured 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 the problems that I had, I added a few bits of code:

My Admin controllers have the correct namespace

  • The PowellCasting.Areas.Admin namespace, not just PowellCasting.
  • I have RegisterAllAreas installed in the global
  • ControllerBuilder.Current.DefaultNamespaces.Add ("PowellCasting / Controllers") is used to indicate the default route.

The specific problem that I am facing right now is that when I go to "/ Reviews", I get the above error from several controllers, in particular:

* The following matching controllers were found in the "reviews" request: PowellCasting.PowellCasting.Areas.Admin.ReviewsController

PowellCasting.PowellCasting.ReviewsController *

I turned on the route debugger and only shows one match:

ah - as a new user, I can not send the 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 am getting this problem.

I read about overloading the maproute method with 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 am sure it should be simple, and I have tried several things - it is very frustrating. Any help would be really appreciated.

My first post here is Hello! :)

+4
source share
3 answers

If you carefully read the error message you receive:

The following controller match was found in the feedback request: PowellCasting.PowellCasting.Areas.Admin.ReviewsController PowellCasting.PowellCasting.ReviewsController

You will notice that PowellCasting repeated twice.

So, in your main registration of the Global.asax route:

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

this assumes that your ReviewController is fundamentally defined as follows:

 Namespace Controllers Public Class ReviewController Inherits System.Web.Mvc.Controller Function Index() As ActionResult Return View() End Function End Class End Namespace 

Note the lack of a PowellCasting prefix in the namespace definition (which is a VB PowellCasting that adds it automatically, I suppose the name of your application is)

and inside AdminAreaRegistration.vb :

 context.MapRoute( _ "Admin_default", _ "Admin/{controller}/{action}/{id}", _ New With {.action = "Index", .id = UrlParameter.Optional}, _ vbNull, _ {"PowellCasting.Areas.Admin.Controllers"} ) 

which assumes your ReviewController in this area is defined as

 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, note the lack of a PowellCasting prefix in the namespace definition.

+5
source

I also had a similar problem.

I had an application in which I had two areas: Common, Products

And when I opened the application, the Common_default route is called, but when I saw RoutesCollection.Routes, it showed me 4 routes. In fact, I defined only 2 routes - one for general, one for products.

After reading the comments above, I just asked me to change the project properties for my site.

I changed the default namespace from "MyWebsite.Web" to "MyWebsite". And this is a trick. My work has been started.

The bottom line will never be "." in the default namespace for your MVC4 website project.

0
source

DealerNetworksAreaRegistration.cs: // Add Region

 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(); 
0
source

All Articles