0

我正在将MVC Extension 与 Autofac 一起使用,但我的区域存在问题。我不确定我做错了什么。

最初我有以下内容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
     );
}

使用此代码,我的区域显示得很好。 http://localhost:19857/Administration显示我的索引视图。

如果我希望 MVC Extensions 为我注册我的路线和区域,那么http://localhost:19857/Administration什么都不显示,只是一个 404 错误。

这是global.asax.cs注册我的路线和区域的更新:

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

我的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
          );
     }
}

为什么我的区域不显示?

更新

当我去http://localhost:19857/Administration然后默认为仪表板控制器索引视图。这是我所在地区的注册码:

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 }
     );
}
4

1 回答 1

1

我似乎已经解决了这个问题。问题是Routes.Clear(); 我把它拿出来了,现在一切正常。这是我对上面代码所做的更改:

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

这是我的更新global.asax.cs

public class MvcApplication : AutofacMvcApplication
{
     public MvcApplication()
     {
          Bootstrapper.BootstrapperTasks
               .Include<RegisterAreas>()
               .Include<RegisterControllers>()
               .Include<RegisterRoutesBootstrapperTask>()
               .Include<AutoMapperBootstrapperTask>();
     }
}
于 2012-05-24T06:46:37.590 回答