MVC areas not diplaying when registering routes and areas with MVC extensions

May 23, 2012 at 4:17 PM
Edited May 23, 2012 at 4:19 PM

Please excuse if this forums is not used, but I am in need of an answer and I do not know where to post it besides on Stack Overflow.

I am using MVC Extension with Autofac and I am having issues with my areas.  I'm not sure what I am doing wrong.

Initially I had the following in my global.asax.cs:

public class MvcApplication : AutofacMvcApplication
{
     public MvcApplication()
     {
          Bootstrapper.BootstrapperTasks
               .Include<RegisterControllers>();
     }
}
protected override void OnStart()
{
     AreaRegistration.RegisterAllAreas();
   
     RegisterGlobalFilters(GlobalFilters.Filters);
     RegisterRoutes(RouteTable.Routes);
   
     base.OnStart();
}
   
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}
   
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 }
    );
}


With this code my areas displayed well.  http://localhost:19857/Administration displays my Index view.

If I want MVC Extensions to register my routes and areas for me then http://localhost:19857/Administration displays nothing, just a 404 error.

This is the updated global.asax.cs to register my routes and areas:

public class MvcApplication : AutofacMvcApplication
{
     public MvcApplication()
     {
          Bootstrapper.BootstrapperTasks
               .Include<RegisterAreas>()
               .Include<RegisterControllers>()
               .Include<RegisterRoutesBootstrapperTask>();
     }
   
     protected override void OnStart()
     {
          RegisterGlobalFilters(GlobalFilters.Filters);
   
          base.OnStart();
     }
   
     public static void RegisterGlobalFilters(GlobalFilterCollection filters)
     {
          filters.Add(new HandleErrorAttribute());
     }
}


My RegisterRoutesBootstrapperTask class:

public class RegisterRoutesBootstrapperTask : RegisterRoutesBase
{
     public RegisterRoutesBootstrapperTask(RouteCollection routes)
          : base(routes)
     {
     }
   
     protected override void Register()
     {
          Routes.Clear();
   
          Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   
          Routes.MapRoute(
               "Default",
               "{controller}/{action}/{id}",
               new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
          );
     }
}


Why won't my areas display?

When I go to http://localhost:19857/Administration then it needs to default to the Dashboard controllers Index view.  Here is my area's registration code:

public override string AreaName
{
     get
     {
          return "Administration";
     }
}
   
public override void RegisterArea(AreaRegistrationContext context)
{
     context.MapRoute(
          "Administration_default",
          "Administration/{controller}/{action}/{id}",
          new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
     );
}
Developer
May 23, 2012 at 8:15 PM

Hi, add

 AreaRegistration.RegisterAllAreas(); 

To RegisterRoutesBootstrapperTask

May 24, 2012 at 6:45 AM

Thanks.

I seem to have sorted out the problem.  The problem is with the Routes.Clear();  I took it out and now everything is working fine.  Here is my changes that I did to the code above:

public class RegisterRoutesBootstrapperTask : RegisterRoutesBase
{
     public RegisterRoutesBootstrapperTask(RouteCollection routes)
          : base(routes)
     {
     }
     protected override void Register()
     {
          Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
          Routes.MapRoute(
               "Default",
               "{controller}/{action}/{id}",
               new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
     }
}

Here is my updated global.asax.cs:

public class MvcApplication : AutofacMvcApplication
{
     public MvcApplication()
     {
          Bootstrapper.BootstrapperTasks
               .Include<RegisterAreas>()
               .Include<RegisterControllers>()
               .Include<RegisterRoutesBootstrapperTask>()
               .Include<AutoMapperBootstrapperTask>();
     }
}