2011-03-02 42 views
12

我試圖提高MVC2應用啓動的速度。提高ASP.NET MVC啓動性能

我做了第一輪的表現採樣,並出現了

MvcAreaRegistration.RegisterAllAreas 

佔用了大部分的啓動時間。

我讀here,你也可以手動註冊該區域,我想嘗試一下,但我不確定該頁面的語法是如何工作的。

所以我的第一個問題是:我怎樣才能手動註冊我的區域?

回答

5

首先準備自己一個輔助方法在Global.asax中是這樣的:

private static void RegisterArea<T>(RouteCollection routes, object state) where T : AreaRegistration 
{ 
    AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(typeof(T)); 
    AreaRegistrationContext registrationContext = new AreaRegistrationContext(registration.AreaName, routes, state); 
    string areaNamespace = registration.GetType().Namespace; 
    if (!String.IsNullOrEmpty(areaNamespace)) 
    registrationContext.Namespaces.Add(areaNamespace + ".*"); 
    registration.RegisterArea(registrationContext); 
} 

現在你可以使用手動註冊這個助手方法的Application_Start這樣的:

//Replace AreaRegistration.RegisterAllAreas(); with lines like those 
RegisterArea<FirstAreaRegistration>(RouteTable.Routes, null); 
RegisterArea<SecondAreaRegistration>(RouteTable.Routes, null); 

的AreaRegistration類當您添加新區域時由Visual Studio創建,您可以在Areas/AreaName目錄中找到它們。

0

您可以完全手動完成此操作,並避免使用RegisterArea實現。

檢查這篇文章: http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/

總之 - 你需要 「區域」 DataToken添加到您的路線:

private void RegisterAreas(RouteCollection routes) 
{ 
    // AreaRegistration.RegisterAllAreas(); 
    var route = routes.MapRoute(
     "MyArea_Default", 
     "MyArea/{controller}/{action}/{id}", 
     new { controller = "App", action = "Index", id = UrlParameter.Optional }, 
     new string[] { "MyProject.Areas.*" } 
    ).DataTokens.Add("Area", "CDR"); 
}