我使用System.Web.Optimization & WebGrease DLL在我的项目中使用以下代码自动执行捆绑和最小化JavaScript和css文件。
using System.Web.Optimization;
using WebGrease;
protected void Application_Start()
{
RegisterBundles(BundleTable.Bundles);
BundleTable.EnableOptimizations = false;
}
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/Content/Script/Common/").Include("~/Content/Scripts/PTax/Common/ViewPort.js",
"~/Content/Scripts/PTax/Common/jquery.rotate.js",
"~/Content/Scripts/PTax/Common/jquery.loupe.js",
"~/Content/Scripts/PTax/Common/Accordion.js",
"~/Content/Scripts/PTax/Common/Progress.js",
"~/Content/Scripts/PTax/Common/AjaxGlobalHandler.js",
"~/Content/Scripts/PTax/Common/DialogBox.js",
"~/Content/Scripts/PTax/Common/Common.js",
"~/Content/Scripts/PTax/Common/DateValidations.js"));
}
在我的项目中,某个模块只有一个javascript文件。我不需要将其与任何其他文件捆绑在一起。所以我只需要它最小化它。我该怎么做。
您可以创建一个新的捆绑包,(创建多个捆绑包是正常的)
bundles.Add(new ScriptBundle("~/bundles/get").Include("~/Scripts/app/home.js"));
然后在必要的视图中呈现脚本
@Scripts.Render("~/bundles/home")