Search code examples
asp.netasp.net-mvcasp.net-mvc-3autofacasp.net-mvc-3-areas

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


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", // Route name
          "{controller}/{action}/{id}", // URL with parameters
          new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
     );
}

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?

UPDATE

When I go to http://localhost:19857/Administration then defaults 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 }
     );
}

Solution

  • 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>();
         }
    }