Question

I have a strange problem working with Areas in ASP.NET MVC 4.

root --- Areas --- Admin      ___ HomeController
                              ___ AgenceController
               --- AdminAgence___ HomeController
                              ___ AgenceController

The Admin Area works perfectly, now when I passed to the AgenceAdmin, it works when I redirect the Home Controller and not working when I redirect to AgenceController or any other Controller, this is my regestration code for both areas :

public class AdminAgenceAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "AdminAgence";
            }
        }

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

And For the Admin Area (wich works perfectly)

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 { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

And the Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }

I made the AdminAgence Area just like the Admin and nothing works for the AdminAgence ...

I also work with roles to define the role to access every area, and the roles area Admin, AdminAgence and Membre

Was it helpful?

Solution 2

You shouldn't have to add anything. Best is that you need to remove all that routing from your existing project that references your new area name, and then let the framework handle all of the routing for you.

OTHER TIPS

Use the MapRoute overload with namespaces in all your mappings

context.MapRoute(
    name: "AdminAgence_default",
    url: "AdminAgence/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new string[] { "MyNamespace.Areas.AdminAgence.Controllers" }
);

Check the area folder name to ensure it matches the route name specified in the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top