4

以前にScottGuthrieが読んだ記事と、 Mads Kristensenが投稿したビデオによると、これを置き換えることで、ASP.netMVC4で自動バンドル/縮小できるはずです。

<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;
 }
4

2 に答える 2

6

はい、この機能はMVC4 RCバージョンでは削除されていますが、RCリリースノートはもう見つかりませんでした。

MVC4ベータからRCへのアップグレードに関するRickAndersonのブログ投稿では、プロセスについて次のように説明しています。

「autobundling」参照を削除BundleConfig.csし、バンドル構成を使用してaを作成/コピーし、Global.asaxから。を使用して呼び出しますBundleConfig.RegisterBundles(BundleTable.Bundles);

ハンゼルマンは、決定に関するいくつかの背景情報に言及しています:

ベータ版以降、Web最適化(縮小およびバンドル)フレームワークにいくつかの重要な変更がありました。ベータ版では、バンドルされているものとその順序を十分に制御できなかったため、完全に制御できるBundleConfig.cs(または.vb)に移動されました。

于 2012-09-03T05:33:15.783 に答える
6

正確ではないため、EnableDefaultBundles「自動バンドル」と呼ばれるものを削除しましたが、基盤となる機能は引き続き存在します。

登録することで、そのメソッドが行っていたのと同等のことを行うことができます。

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

アルファベット順は通常望ましい順序ではないため、これはかなり問題のあるアプローチであるため、このメソッドを削除しました。

于 2012-09-04T09:38:51.240 に答える