【问题标题】:Automate minfication using System.Web.Optimization & WebGrease in asp.net mvc without bundling在 asp.net mvc 中使用 System.Web.Optimization 和 WebGrease 自动缩小,无需捆绑
【发布时间】:2014-03-19 10:28:29
【问题描述】:

我使用 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 文件。我不需要将它与任何其他文件捆绑在一起。所以我只需要它缩小它。我该怎么做。

【问题讨论】:

标签: asp.net asp.net-mvc bundling-and-minification webgrease


【解决方案1】:

你可以新建一个bundle,(创建多个bundle是正常的)

bundles.Add(new ScriptBundle("~/bundles/get").Include("~/Scripts/app/home.js"));

然后在必要的视图中渲染脚本

@Scripts.Render("~/bundles/home")

【讨论】:

  • 我可以在相应的视图中自动缩小而不像你在 global.asax 文件中所说的那样添加行吗?
  • 据我所知,webgrease 会获取您指定的文件并进行缩小并将它们合并为 1,如果您不指定文件,webgrease 怎么知道要捆绑和缩小什么?在你的某些观点中,应该有~/Content/Script/Common/?您还可以在视图的文件夹中指定另一个 _Layouts.cshtml 并排除公共包
猜你喜欢
  • 1970-01-01
  • 2013-09-24
  • 2013-02-21
  • 1970-01-01
  • 2019-06-28
  • 2017-12-12
  • 2014-04-16
  • 2014-11-26
  • 2019-03-05
相关资源
最近更新 更多