Areas in asp.net mvc, only one work at time

2.1k views Asked by At

My folder look like this:

  • (root)/Areas/Admin/Views/..
  • (root)/Areas/Admin/Controllers/...
  • (root)/Areas/Admin/Routes.cs

  • (root)/Areas/Forum/Views/..

  • (root)/Areas/Forum/Controllers/...
  • (root)/Areas/Forum/Routes.cs

    public class Routes : AreaRegistration
    

    { public override string AreaName { get { return "Admin"; } }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_Default",
            "{controller}/{action}/{Id}",
            new { controller = "Admin", action = "Index", Id = (string)null }
        );
    }
    

    }

    public class Routes : AreaRegistration { public override string AreaName { get { return "Forum"; } }

    public override void RegisterArea(AreaRegistrationContext routes)
    {
        routes.MapRoute(
            "Forum_Default",
            "{controller}/{action}",
            new { controller = "Forum", action = "Index"}
        );
    }
    

    }

Global.asax

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        AreaRegistration.RegisterAllAreas();

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

The startpage should be Home/Index but it start with Admin/Index, why?

Only site.com/Admin works not site.com/Forum

How should i get Admin and Forum Areas to work right? Why is only Admin working and not Forum?

When i delete Admin/Routes.cs file Forum start to work...

EDIT:

Home in ~/Views/ don't show as startpage even if i have

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

in my Global.asax after AreaRegistration.RegisterAllAreas();

1

There are 1 answers

0
LiamB On BEST ANSWER

I believe your area mappings should be structured like so.

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_Default",
        "Admin/{controller}/{action}/{Id}",
        new { controller = "Admin", action = "Index", Id = (string)null }
    );
}

and

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Forum_Default",
        "Forum/{controller}/{action}/{Id}",
         new { controller = "Forum", action = "Index"}
    );
}

Keeps your routes from conflicting, which is what i think is happening in your case. As your default route matches your admin route.