0

我正在使用MVC Extension with Autofac,我遇到了與我的區域有關的問題。我不知道我做錯了什麼。當註冊MVC擴展的路由和區域時,MVC區域並未停頓

最初我曾在我的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擴展爲我註冊我的路線和區域,那麼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 
     ); 
    } 
} 

爲什麼不是我的領域顯示?

UPDATE

當我去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 } 
    ); 
} 
+0

如果你訪問'的http://本地主機:19857 /行政/主頁/ Index' (或者你的默認區域控制器中的動作)? – jgauffin

+0

請參閱我的更新。以上URL使用管理結構中的Dashboard控制器的Index視圖。 –

+0

是的,但你可以瀏覽'http:// localhost:19857/Administration/dashboard/index /',或者你也得到404嗎? – jgauffin

回答

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