Area folders look like :
Areas
Admin
Controllers
UserController
BranchController
AdminHomeController
Project directories look like :
Controller
UserController
GetAllUsers
area route registration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" }
);
}
project route registration
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Areas.Admin.Controllers" });
}
When I route like this: http://mydomain.com/User/GetAllUsers
I get resource not found error (404). I get this error after adding UserController to Area.
How can I fix this error?
Thanks...
Areas allows you to separate your modules and organize Model, View, Controller, Web. config and Routing registration file into separate sections. In live MVC Project implementation we can use Areas concept for organizing project in better manageable way. Area separate logical section like Model, View, Controller, Web.
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.
The routing area, abbreviated as RA, is the counterpart of the location area (LA) in packet-switched (PA) networks. The RA is usually a smaller area compared to the LA because using multimedia services requires more frequent paging messages.
In the application startup code, you can call AreaRegistration. RegisterAllAreas() to register all areas. This needs a routing cs file for each area as described in the preceding article. Once registered, all the areas will start showing up in the Routing Table.
You've messed up your controller namespaces.
Your main route definition should be:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Controllers" }
);
And your Admin area route registration should be:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
}
Notice how the correct namespaces should be used.
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