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
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.
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).
The three segments of a default route contain the Controller, Action and Id.
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).
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.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With