【问题标题】:Did Auto-Bundling/Minification Get Tossed in Final Release of MVC 4?自动捆绑/缩小在 MVC 4 的最终版本中被抛弃了吗?
【发布时间】:2012-09-02 20:43:09
【问题描述】:

根据article I read earlier by Scott Guthrievideo posted by Mads Kristensen,我应该能够通过替换此代码在 ASP.net MVC 4 中自动捆绑/缩小:

<link href="Styles/reset.css" rel="stylesheet" type="text/css" />
<link href="Styles/normalize.css" rel="stylesheet" type="text/css" />
<link href="Styles/styles.css" rel="stylesheet" type="text/css" />

<script src="Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-validation.min.js" type="text/javascript"></script>

有了这个:

<link href="Styles/css" rel="stylesheet" type="text/css" />

<script src="Scripts/js" type="text/javascript"></script>

我尝试同时针对 .Net 4.0 和 4.5,但似乎没有什么不同。我收到 404 错误,并且链接和脚本标签永远不会指向捆绑的资源。

此功能是否已从最终版本中删除?

我想将此功能用于“主题”。


这就是我最终实施的方式。希望这是有道理的......

 /// <summary>
 /// Render stylesheets HTML for the given theme. Utilizes System.Web.Optimization for bundling and minification
 /// </summary>
 /// <param name="themeName">Name of theme</param>
 /// <returns>HtmlString containing link elements</returns>
 public static IHtmlString RenderThemeStyles(string themeName)
 {
     IHtmlString retValue = null;

     // If no theme name is passed, return null
     if (!themeName.HasValue())
         return retValue;

     var ctxt = HttpContext.Current;
     string themePath = "~/Themes/" + themeName;
     string themeKey = themePath + "/css";

     if (ctxt.Cache[themeKey] != null)
         return (IHtmlString)ctxt.Cache[themeKey];

     // Check to see if the theme directory exists. Throw error if it does not
     string themeSysPath = HttpContext.Current.Server.MapPath(themePath);
     DirectoryInfo themeDir = new DirectoryInfo(themeSysPath);
     if (!themeDir.Exists)
         throw new ApplicationException(string.Format("Theme directory \"{0}\" does not exist", themePath));

     // Remove the old bundle if it already exists
     var old_bundle = BundleTable.Bundles.FirstOrDefault(b => b.Path == themeKey);
     if (old_bundle != null)
         BundleTable.Bundles.Remove(old_bundle);

     if (themeDir.GetFiles("*.css").Length > 0)
     {
         // If there are css files, add them to the bundler and save the rendered output to cache
         Bundle styles = new StyleBundle(themeKey).IncludeDirectory(themePath, "*.css");
         BundleTable.Bundles.Add(styles);
         retValue = Styles.Render(themeKey);
         ctxt.Cache.Insert(themeKey, retValue, new System.Web.Caching.CacheDependency(themeSysPath));
     }
     else
     {
         // If there are no css files, save empty string to cache
         ctxt.Cache.Insert(themeKey, new HtmlString(string.Empty), new System.Web.Caching.CacheDependency(themeSysPath));
     }

     return retValue;
 }

【问题讨论】:

  • 是的,这个功能在 RC 版本中被删除了,虽然我找不到任何关于它的公告。只有Hanselman 提到它:“自测试版以来,Web 优化(缩小和捆绑)框架发生了一些重大变化。对于捆绑的内容和测试版中的顺序没有足够的控制,因此已将其移至您可以完全控制的 BundleConfig.cs(或 .vb)”
  • nemesv。添加为答案,我会接受。有点希望他们能保留自动的东西,并允许人们在 BundleConfig 中指定他们是否需要这种类型的粒度。现在我必须考虑重新创建类似的东西,或者弄清楚如何使用对我的主题文件夹的缓存依赖项动态地执行捆绑包。
  • 您仍然可以使用IncludeDirectory 调用添加整个文件夹,您甚至可以使用通配符参见Bundling and Minification - Creating a Bundle
  • stackoverflow.com/questions/20347098/… ,可能正在形成一些势头以将其包含在 SquishIt 中

标签: asp.net-mvc asp.net-mvc-4 asp.net-optimization bundling-and-minification


【解决方案1】:

是的,这个功能在 MVC4 RC 版本中被删除了,尽管我再也找不到 RC 发行说明了。

Rick Anderson 在upgrade MVC4 beta to RC 上的博文描述了这个过程:

删除“自动捆绑”引用并使用您的捆绑配置创建/复制BundleConfig.cs,并使用BundleConfig.RegisterBundles(BundleTable.Bundles); 从 Global.asax 调用它。

Hanselman 提到了一些backround info 的决定:

网络优化发生了一些重大变化 自测试版以来的(缩小和捆绑)框架。没有足够的 控制捆绑的内容以及测试版中的顺序,这就是 已移至您拥有总计的 BundleConfig.cs(或 .vb)中 控制

【讨论】:

    【解决方案2】:

    不完全是,所以我们删除了EnableDefaultBundles,即您所说的“自动捆绑”,但底层功能仍然存在。

    您可以通过注册来执行与该方法相同的操作:

    BundleTable.Bundles.Add(new DynamicFolderBundle("js", "*.js");
    BundleTable.Bundles.Add(new DynamicFolderBundle("css", "*.css");
    

    我们删除了该方法,因为这是一个相当有问题的方法,因为字母顺序通常不是所需的顺序。

    【讨论】:

    • 是的,但似乎不可能将dynamicFolderBundle 放在我的Includes 之后。我的意思是-目前我正在使用bundles.Add(new DynamicFolderBundle("userScripts", "*.js",new JsMinify()).Include("~/Scripts/YGeneral.js").Include("~/Scripts/ZGeneral.js")) - 。但由于某种原因,它首先将DynamicFolderBundle输出中的脚本放在首位,然后将其他包含。有什么办法可以先放Includes,然后放DynamicFolderBundle
    猜你喜欢
    • 2013-02-21
    • 2012-05-23
    • 1970-01-01
    • 2018-06-21
    • 2019-06-28
    • 2014-11-26
    • 1970-01-01
    • 2011-09-11
    • 2015-07-31
    相关资源
    最近更新 更多