MVC4 Default Route points to an area

There are 2 areas in my project:

Areas | Admin Areas | FrontEnd 

I would like that when visiting the site, the default route should load controllers / views / models from the FrontEnd . It's Url/Admin have Url/Admin for the Url/Admin panel, but I would prefer not to force Url/FrontEnd (or some other options). Basically, I don't want to use the Controller / Model / View folders at the root level.

I am not sure how to change the code to allow this or even this is a sensible method. Can someone give some recommendations?

What I have:

 routes.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { area = "Admin", controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new[] { "WebsiteEngine.Areas.Admin.Controllers" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { area = "FrontEnd", controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new[] { "WebsiteEngine.Areas.FrontEnd.Controllers" } ); 

However, this causes an error:

 The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/Home/Index.cshtml ~/Views/Home/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml 

I have views available in areas, and it doesn't look like it looks there.

+8
asp.net-mvc-4 routing
source share
1 answer

I believe that you can just do something like this:

 // Areas/Admin/AdminAreaRegistration.cs public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( name: "Admin_Default", url: "Admin/{controller}/{action}/{id}", defaults: new { area = "Admin", controller = "Home", action = "Index", id = UrlParameter.Optional }); } } // Areas/Admin/FrontEndAreaRegistration.cs public class FrontEndAreaRegistration : AreaRegistration { public override string AreaName { get { return "FrontEnd"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( name: "FrontEnd_Default", url: "{controller}/{action}/{id}", defaults: new { area = "FrontEnd", controller = "Home", action = "Index", id = UrlParameter.Optional }); } } // Global.asax.cs protected void Application_Start() { AreaRegistration.RegisterAllAreas(); ... } 

Now, in your RouteConfig class, you probably configured the Default route. Keep in mind that as long as you call AreaRegistration.RegisterAllAreas before you call RouteConfig.RegisterRoutes , the routes that you configured in the areas can override the routes configured in RouteConfig . (Routes are evaluated in the order they appear in the Routes collection, and .MapRoute pushes new routes to the end)

+10
source share

All Articles