0

バンドル パスがルート フォルダに対して相対的でない場合に、Script.Render がコンパイル用のバンドルを無効にしない理由を知っている人はいますか?

次のようなルートへの相対パスを使用してバンドルを作成しています (このパス タイプは必須です。そうしないと、例外がスローされます)。

Bundle jsBundle = new ScriptBundle("~/bundles/myscripts/");

しかし、レンダリングしようとすると、以下のような完全な URL を提供する必要があります。

Scripts.Render("http://myserver/bundles/myscripts/")

また、バンドルはコンパイルのデバッグ モードに関係なく有効になります。

私が見逃しているアイデアはありますか?

私の質問はこの質問に非常に関連しています-私はバンドルをそのようにレンダリングしています-今:コンパイル時に debug="true" を無効にするにはどうすればよいですか?

何か案は ?

ありがとう!オヴィ

4

1 に答える 1

1

私自身の質問に答えるには: Scripts.Render は、バンドルの URL が次のような完全な URL として提供されている場合、コンパイル モードに応じてバンドルを切り替えません。

Scripts.Render("http://myserver/bundles/myscripts/")

私がとったアプローチは、バンドルをレンダリングする独自の mvc ヘルパーを作成することでした。

public MvcHtmlString BundleScript(string bundleUrl)
{
        var javascriptBuilder = new StringBuilder();
        bool filesExist = false;
        bool isDynamicEnabled = IsDynamicEnabled();

        if (!isDynamicEnabled)
        {
            IEnumerable<string> fileUrls = GetBundleFilesCollection(bundleUrl);
            string rootVirtualDirectory = "~/content/js/";

            if (fileUrls != null)
            {
                foreach (string fileUrl in fileUrls)
                {
                    javascriptBuilder.Append(new ScriptTag().WithSource(GetScriptName(fileUrl, rootVirtualDirectory)).ToHtmlString());
                }
                filesExist = true;
            }
        }

        if (isDynamicEnabled || !filesExist)
        {
            javascriptBuilder.Append(new ScriptTag().WithSource(bundleUrl).ToHtmlString());
        }
        return MvcHtmlString.Create(javascriptBuilder.ToString());
}

private IEnumerable<string> GetBundleFilesCollection(string bundleVirtualPath)
{
        var collection = new BundleCollection { BundleTable.Bundles.GetBundleFor(bundleVirtualPath) };
        var bundleResolver = new BundleResolver(collection);
        return bundleResolver.GetBundleContents(bundleVirtualPath);
}

private bool IsDynamicEnabled()
{
        return BundleTable.EnableOptimizations;
}
private static string GetScriptName(string scriptUrl, string virtualDirectory)
{
        return scriptUrl.Replace(virtualDirectory, string.Empty);
}
于 2014-03-07T06:38:58.043 に答える