Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 Default route when using areas

I'm trying to use areas within MVC app, I would like that the default route will be resolved to the HomeController within the admin area but it resolves to the home controller in root site. I added the namespace of the admin HomeController but it still resolved to the root HomeController.

My Route config:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new {controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] {"MvcApplicationAreas.Areas.Admin.Controllers"}

        );
    }
}

admin area route

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}

HomeController - Admin area

namespace MvcApplicationAreas.Areas.Admin.Controllers
{
   public class HomeController : Controller
   {

       public ActionResult Index()
       {
           return View();
       }

   }
}

Any idea why it wouldn't resolve properly? Thanks

like image 282
spooti Avatar asked Jun 20 '13 17:06

spooti


People also ask

What is the default route pattern 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.

What is the default setting for route config in MVC?

Routing in ASP.NET MVC By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).

What are the three segments for routing important?

The three segments of a default route contain the Controller, Action and Id.

How to change the default route in MVC 4?

This blog suggests the way to change the default route in ASP.NET MVC4. As we know that ASP.NET MVC by default takes following setting available in RouteConfig file (under App_Start folder). This means when you run the application, Home controller will be invoked and it's Index method will be executed by taking parameter id (if any).

Which file contains the default route table for an MVC application?

The file in Listing 1 contains the default Global.asax file for an ASP.NET MVC application. Listing 1 - Global.asax.cs When an MVC application first starts, the Application_Start () method is called. This method, in turn, calls the RegisterRoutes () method. The RegisterRoutes () method creates the route table.

How do I route a URL in MVC?

Routing in MVC If the URL doesn't contain anything after the domain name, then the default controller and action method will handle the request. For example, http://localhost:1234 would be handled by the HomeController and the Index() method as configured in the default parameter.

What is the routing process in MVC?

The following figure illustrates the Routing process. Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig.cs under App_Start folder. The following figure illustrates how to configure a route in the RouteConfig class .


2 Answers

The most straightforward way is to add a data token to your default route:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new {controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens.Add("area", "Admin");

Simply add

.DataTokens.Add("area", "[your area name]");

to the end of your default route definition.

like image 195
NovaJoe Avatar answered Oct 12 '22 08:10

NovaJoe


I've tested your code for the area registration and it works and selects the right controller. However, the view resolution finds the view in the root folder, even if the right controller is used.

To test, I used the following home index action in my areas home controller:

public ActionResult Index()
{
    return View(model: "Admin area home controller");
}

Then my index.chstml in /Views/Home:

Root View: @Model

and my index.cshtml in /Areas/Admin/Views/Home:

Admin Area view: @Model

When run, the output is:

Root View: Admin area home controller

So the route makes the home controller in the admin area run, but then thew view resolution goes on and finds the root view instead of the admin view.

So in the end, it is indeed the view selection that is wrong, so your problem is the same as in How to set a Default Route (To an Area) in MVC.

like image 43
Anders Abel Avatar answered Oct 12 '22 07:10

Anders Abel