2015-09-14 40 views
3

我想知道捆綁和縮小在服務器上運行的時間有多可能?ASP.NET捆綁和縮小。每個請求運行一次?

是否每個HTML請求都有一次? 每個瀏覽器會話一次? 每次應用程序部署一次?當應用程序被部署或重新啓動,當Application_Start方法被稱爲在Global.asax.cs

由於創建

+0

每次應用池回收 – ediblecode

回答

3

束。在Application_Start內部,BundleConfig.RegisterBundles被調用,這實際上是魔術發生的地方。

public class MvcApplication : System.Web.HttpApplication 
{ 
    // this method is called on application start 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); // registers areas 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); // registers filters 
     RouteConfig.RegisterRoutes(RouteTable.Routes); // registers routes 
     BundleConfig.RegisterBundles(BundleTable.Bundles); // this generates the bundles 
    } 
} 

而在你BundleConfig.cs文件時,RegisterBundles方法是調用來創建方法,該束。

public class BundleConfig 
{ 
    public static void RegisterBundles(BundleCollection bundles) 
    { 
     // this is actually what's creating the bundles 
     bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/jquery-{version}.js")); 
     // etc... 
    } 
} 

然後把它們存儲在內存中,可以在/bundles/bundlename?v=versionId訪問,並擔任每個HTTP請求,但捆綁和微小的實際過程只發生一次。

2

捆綁微小的Application_Start註冊運行一次,結果被緩存在服務器上。緩存版本提供了其他請求。 Aplication_Start在請求第一個資源時觸發。