10

構成に基づいて CSS ファイルと JS ファイルを追加する動的バンドルを使用しています。

次のような新しい StyleBundle をスピンアップします。

var cssBundle = new StyleBundle("~/bundle/css");

次に、構成をループして、見つかったインクルードを追加します。

cssBundle.Include(config.Source);

ループに続いて、実際にファイル/ディレクトリが含まれているかどうかを確認したいと思います。EnumerateFiles() があることは知っていますが、これが 100% 目的を果たしているとは思いません。

他の誰かが以前に似たようなことをしましたか?

4

2 に答える 2

16

このBundleクラスは、アプリケーションに公開されていない内部項目リストを使用しており、必ずしもリフレクション経由でアクセスできるとは限りません (試してみましたが、コンテンツを取得できませんでした)BundleResolver次のようにクラスを使用して、これに関する情報を取得できます。

var cssBundle = new StyleBundle("~/bundle/css");
cssBundle.Include(config.Source);

// if your bundle is already in BundleTable.Bundles list, use that.  Otherwise...
var collection = new BundleCollection();
collection.Add(cssBundle)

// get bundle contents
var resolver = new BundleResolver(collection);
List<string> cont = resolver.GetBundleContents("~/bundle/css").ToList();

カウントが必要な場合は、次のようにします。

int count = resolver.GetBundleContents("~/bundle/css").Count();

編集:リフレクションの使用

どうやら私は以前の反省テストで何か間違ったことをしたようです。

これは実際に機能します:

using System.Reflection;
using System.Web.Optimization;

...

int count = ((ItemRegistry)typeof(Bundle).GetProperty("Items", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cssBundle, null)).Count;

もちろん、そこにいくつかの安全性チェックを追加する必要があります。多くのリフレクションの例と同様に、これはItemsプロパティの意図された安全性に違反しますが、機能します。

于 2013-02-15T05:20:56.823 に答える
1

には次の拡張メソッドを使用できますBundle

public static class BundleHelper
{
    private static Dictionary<Bundle, List<string>> bundleIncludes = new Dictionary<Bundle, List<string>>();
    private static Dictionary<Bundle, List<string>> bundleFiles = new Dictionary<Bundle, List<string>>();

    private static void EnumerateFiles(Bundle bundle, string virtualPath)
    {
        if (bundleIncludes.ContainsKey(bundle))
            bundleIncludes[bundle].Add(virtualPath);
        else
            bundleIncludes.Add(bundle, new List<string> { virtualPath });

        int i = virtualPath.LastIndexOf('/');
        string path = HostingEnvironment.MapPath(virtualPath.Substring(0, i));

        if (Directory.Exists(path))
        {
            string fileName = virtualPath.Substring(i + 1);
            IEnumerable<string> fileList;

            if (fileName.Contains("{version}"))
            {
                var re = new Regex(fileName.Replace(".", @"\.").Replace("{version}", @"(\d+(?:\.\d+){1,3})"));
                fileName = fileName.Replace("{version}", "*");
                fileList = Directory.EnumerateFiles(path, fileName).Where(file => re.IsMatch(file));
            }
            else // fileName may contain '*'
                fileList = Directory.EnumerateFiles(path, fileName);

            if (bundleFiles.ContainsKey(bundle))
                bundleFiles[bundle].AddRange(fileList);
            else
                bundleFiles.Add(bundle, fileList.ToList());
        }
    }

    public static Bundle Add(this Bundle bundle, params string[] virtualPaths)
    {
        foreach (string virtualPath in virtualPaths)
            EnumerateFiles(bundle, virtualPath);

        return bundle.Include(virtualPaths);
    }

    public static Bundle Add(this Bundle bundle, string virtualPath, params IItemTransform[] transforms)
    {
        EnumerateFiles(bundle, virtualPath);
        return bundle.Include(virtualPath, transforms);
    }

    public static IEnumerable<string> EnumerateIncludes(this Bundle bundle)
    {
        return bundleIncludes[bundle];
    }

    public static IEnumerable<string> EnumerateFiles(this Bundle bundle)
    {
        return bundleFiles[bundle];
    }
}

Include()次に、呼び出しをAdd()次のように置き換えます。

var bundle = new ScriptBundle("~/test")
    .Add("~/Scripts/jquery/jquery-{version}.js")
    .Add("~/Scripts/lib*")
    .Add("~/Scripts/model.js")
    );

var includes = bundle.EnumerateIncludes();
var files = bundle.EnumerateFiles();

も使用している場合は、それぞれの拡張メソッドIncludeDirectory()を追加して例を完成させてください。AddDirectory()

于 2014-09-12T11:16:09.240 に答える