0

Does any of you know why the Script.Render is not disabling the bundle for compilation debug ="true" when the bundle path is not relative to the root folder ?

I am creating the bundle using a relative path to the root like (this path type is mandatory, otherwise an exception will be thrown):

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

But when I try to render it I need to provide the full url like below:

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

And the bundle is enabled irrespective of compilation debug mode.

Any ideas what I am missing?

My question is very related to this question - I am rendering my bundle that way - now: how can I make it disabled when compilation debug="true" ?

Any ideas ?

Thanks! Ovi

Community
  • 1
  • 1
Ovi O
  • 43
  • 6
  • What is your code when you try to provide the relative path ? – binard Feb 27 '14 at 13:14
  • Hi jbbi, when I create the bundle in Application_Start is the first line of code from the question, when I try to render it is something like `<%: Scripts.Render("protocol://myserver/bundles/vxscripts/") %>`. I am using this syntax because the bundle goes through e CDN and I need the full URL. – Ovi O Feb 27 '14 at 13:37
  • but anyhow, once I don't use the ~ character in the Scripts.Render, I cannot disable anymore the bundle using compilation debug mode. This is my question, how can I customize this behavior to be able to use partial paths or full URL for the path and still have the compilation debug mode deciding if the bundling is enabled or not. – Ovi O Feb 27 '14 at 13:40
  • Aren't you forcing it somewhere like this BundleTable.EnableOptimizations = true; ? – Ondrej Svejdar Feb 27 '14 at 15:11
  • Ondrej, no I don't use EnableOptimizations=true. – Ovi O Feb 27 '14 at 15:17

1 Answers1

1

To answer to my own question: Scripts.Render doesn't toggle the bundling depending on compilation mode if the bundle url is provided as full url like:

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

The approach I took was to create my own mvc helper to render the bundle:

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);
}
Ovi O
  • 43
  • 6