Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvc area routing?

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...

like image 698
AliRıza Adıyahşi Avatar asked Mar 25 '13 13:03

AliRıza Adıyahşi


People also ask

What are MVC areas?

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.

What are MVC routing?

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.

What is area routing?

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.

How can I register my area in MVC?

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.


1 Answers

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.

like image 70
Darin Dimitrov Avatar answered Oct 02 '22 14:10

Darin Dimitrov