Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same controllername in differents areas

Tags:

asp.net-mvc

In a ASP.NET MVC 3 application with areas (see schema below). The "Controllers" directory from the root has been deleted.

When I do this :

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

I get this error : Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. The request for 'Home' has found the following matching controllers: MyProject.Areas.Administration.Controllers.HomeController MyProject.Areas.BackEnd.Controllers.HomeController MyProject.Areas.FrontEnd.Controllers.HomeController

When I do this :

    routes.MapRoute(
         "Default", // Route name
         "{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
         new string[] { "MyProject.Areas.Administration.Controllers" }
    );

I get tis 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


---- Areas
       |
       |---- Administration
       |       |--- Controllers
       |       |      |---- HomeController
       |       |--- Views
       |              |--- Index
       |---- FrontEnd
       |       |--- Controllers
       |       |      |---- HomeController
       |       |--- Views
       |              |--- Index
       |---- BackEnd
               |--- Controllers
               |      |---- HomeController
               |--- Views
                      |--- Index

Update1 To start to a specific controller in the Areas, I tried this :

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[] { "MyProject.Areas.BackEnd.Controllers" }
    );
}
like image 317
Kris-I Avatar asked Feb 04 '12 22:02

Kris-I


Video Answer


1 Answers

Try the following:

~/Global.asax.cs:

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[] { "MyProject.Controllers" }
    );
}

~/Areas/Administration/AdministrationAreaRegistration.cs

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Administration_default",
        "Administration/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Areas.Administration.Controllers" }
    );
}

~/Areas/FrontEnd/FrontEndAreaRegistration.cs:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "FrontEnd_default",
        "FrontEnd/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Areas.FrontEnd.Controllers" }
    );
}

Now when you request /Administration/Home/Index, the Index action of HomeController in the Administration area will be invoked and it will look for the ~/Areas/Administration/Views/Home/Index.cshtml view. Make sure this view is present at this location. In your picture you seem to have omitted the Home directory - ~/Areas/Administration/Views/Index.cshtml.

like image 163
Darin Dimitrov Avatar answered Oct 22 '22 05:10

Darin Dimitrov