由于之前接触Asp.Net MVC时看的速成版教程,并没有完善学习,断断续续发现了一些新知识,予以记录。
最近发现某个ABP项目种有配置BudleConfig文件,不太知道其作用。百度查明:主要可以压缩Js、Style文件
BudleConfig.cs文件默认在App_Start文件夹中
public class BundleConfig { // For more information on bundling, visit https://go.microsoft.com/fwlink/?LinkId=301862 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js")); //...省略 BundleTable.EnableOptimizations = true; //是否打包压缩 } }
在Global.asax 文件Application_Start()方法中调用了BudleConfig类中RegisterBundles方法。
BundleConfig.RegisterBundles(BundleTable.Bundles);
在Web.config中,当compilation的debug属性设为true时,表示项目处于调试模式,这时Bundle是不会将文件进行打包压缩的。
<system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> <httpModules> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> </httpModules> </system.web>
视图中调用方法:
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/bootstrap")
补充:“~/Scripts/jquery-{version}.js”,{version}匹配对应文件的任何版本并通过工程配置文件选择正常版本还是缩小版,具体是web.config中的debug设置,如果为true选择正常版本,false则是缩小版。需要注意的是我们不能把相同文件的不同版本号放在一起,比如“jquery-1.7.2.js”和“jquery-1.7.1.js”,两个版本号都会被发送给客户端反而造成混淆。