Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 Default Route to point to an area

I have 2 areas in my project:

Areas | Admin
Areas | FrontEnd

What I would like is when I visit the site, the default route should load Controllers / Views / Models from the FrontEnd area. It's normal to have Url/Admin for an admin panel but I would rather not have to force Url/FrontEnd (or some other variation). Basically I don't want to use the Controller / Model / View folders on the root level.

I'm not sure how to change the code to allow this or even it's an advisable method. Could someone provide some guidance please?

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 produces 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 do have views available in the areas and this doesn't look like it's looking there.

like image 387
webnoob Avatar asked Sep 26 '13 21:09

webnoob


People also ask

What is the default route in MVC?

The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home.

How routing is done in the MVC pattern?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig.


1 Answers

I believe 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 have a Default route set up. Bear in mind that as long as you call AreaRegistration.RegisterAllAreas before you call RouteConfig.RegisterRoutes, the routes that you set up in the areas may override the routes you set up in RouteConfig. (Routes are evaluated in the order they appear in the Routes collection, and .MapRoute pushes new routes to the end)

like image 90
p.s.w.g Avatar answered Nov 18 '22 02:11

p.s.w.g