MVC routing with different zones with multiple controllers

My solution structure is as follows:

Areas - Games -Controllers -Views etc - Movies -Controllers - MoviesController.cs - MovieCalendarController.cs - MovieSearchController.cs -Views etc 

Now I would like to do this: Go to https: // localhost / Movies / and click on the MoviesController.cs index

Go to https: // localhost / Movies / Calendar / and click the MovieCalendarController.cs index

Finally, go to https: // localhost / Movies / Search / and click on the MovieSearchController.cs index

What I tried but does not work (I get No route in the route table matches the supplied values. ) Errors:

MovieAreaRegistration.cs

 public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Movies_default", "Movies/{action}/{id}", new { controller = "Movies", action = "Index", id = UrlParameter.Optional } ); context.MapRoute( "Calendar_default", "Movies/Calendar/", new { controller = "MovieCalendar", action = "Index", id = UrlParameter.Optional } ); context.MapRoute( "Search_default", "Movies/Search/{action}/{id}", new { controller = "MovieSearch", action = "Index", id = UrlParameter.Optional } ); } 

Apologies, I'm new to areas and routing

Refresh

After using attribute routing, I got into this problem:

Several types of controllers matching URLs were found. This can happen if the attribute routes on multiple controllers match the requested URL.

The following controller types were found in the request: MovieCalendar.UI.Areas.Movies.Controllers.MovieCalendarController MovieCalendar.UI.Areas.Movies.Controllers.MoviesController

Video controller

 [RouteArea("Movies")] [Route("{action}")] public class MoviesController : BaseController { } 

Calendar controller

 [RouteArea("Movies")] [RoutePrefix("Calendar")] [Route("{action=Index}")] public class MovieCalendarController : BaseController { } 

This happens when accessing the URL http: // localhost / Movies / Calendar in the hope that this will lead me to the MovieCalendarController index action method. I can understand why it complains, because there may be an ActionMethod in a MovieController called Calendar (not there).

+7
c # asp.net-mvc
source share
1 answer

You might be better off with attribute routing. This will allow you to do this:

  public class MoviesController : Controller { [Route("Movies")] public ActionResult Index() { return this.View(); } } public class MovieCalendarController : Controller { [Route("Movies/Calendar")] public ActionResult Index() { return this.View(); } } 

And then you can get rid of your current route mappings and use this to initialize your routes:

 RouteTable.Routes.MapMvcAttributeRoutes(); 

More information on attribute routing can be found here .

Update

 [RouteArea("Movies")] [Route("{action}")] public class MoviesController : BaseController { } 

This route will match URLs starting with Movies / followed by any line, including Calendar. Thus, this route will encounter:

 [RouteArea("Movies")] [RoutePrefix("Calendar")] [Route("{action=Index}")] public class MovieCalendarController : BaseController { } 

Protocol-based routing will be complicated using the naming convention you use for your controllers.

+2
source share

All Articles