Anyone using the new Bundling Optimization from .NET 4.5? - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Anyone using the new Bundling Optimization from .NET 4.5?

  • Anyone using the new Bundling Optimization from .NET 4.5?
  • I have been reading articles on using the new bundling features with .NET 4.5 and am wondering what type of workflow Sitefinity developers might be using with this, if at all.

    I have read numerous articles for both MVC and webform usage, but I am still not sure what the best way to set this up for Sitefinity might be.

    Do I just use NuGet and install the bundle package to my Sitefinity site?  Then I assume I would setup a global.asax file and create the Bundle.config file?

    How well does this all work with css in the themes folders under App_Data? 

    Edit:  I forgot to mention that I am looking to find out if this is possible to use and still allow Sitefinity to automatically render the css from the registered theme or if I would have to explicitly add the styles on the masterpage template and not assign it to a theme to prevent duplicate copies of all the css files.

    So for instance a web form app would use this syntax:  I know I can still bundle any custom js I do like below, but this example is more about the CSS portion.
    <asp:PlaceHolder runat="server">    
          <%: Scripts.Render("~/bundles/modernizr") %>
    <webopt:BundleReference runat="server" Path="~/Content/css" />

    Would I still need to do something like this on my masterpage or is it possible to have the css minified and bundled via the Bundle.Config file and then still let Sitefinity use its cssLoadOrder.xml file to render the bundles into the templates?

  • Hi Stacey,

    That's a good one. I normally don't use the structure that Sitefinity describes in their documentation. What I do is:

    - Store the masterpages inside ~/App_Master
    - Store css, images and scripts in ~/Assets/Css, ~/Assets/Scripts etc.
    - Use the Bundling Optimization framework to render the files from the masterpages, like this:

    protected void Page_Load(object sender, EventArgs e)
                // Add css files
                // Add javascript files
                // Set the homepage link
                lnkHome.NavigateUrl = Helper.GetHomePageUrl(Thread.CurrentThread.CurrentCulture, true);

    The two voids are taking care of the bundling:
    /// <summary>
            /// Add javascript references to the header
            /// </summary>
            /// <param name="relativeScriptPath"></param>
            protected void AddJsScriptReferenceToHead(string relativeScriptPath)
                if (SystemManager.IsDesignMode)
                var scripts = Scripts.Render("~/Assets/Scripts/js").ToHtmlString();
                Page.Header.Controls.Add(new Literal() Text = scripts );
            /// <summary>
            /// Add css references to the header
            /// </summary>
            /// <param name="relativeCssPath"></param>
            protected void AddCssReference(string relativeCssPath)
                var styles = Styles.Render("~/Assets/Styles/css").ToHtmlString();
                Page.Header.Controls.Add(new Literal() Text = styles );

    Maybe it is of any help. If you setup the bundles inside the global.asax, then you could just refer to them from the markup files, even if you are using the Sitefinity structure.

  • Hi Stacey,

    Did you make any progess with this? I've just tried, firstly I get this error:

    [ArgumentException: File "/bundles/plugins" does not exist.
    Parameter name: virtualPath]
    Telerik.Sitefinity.Abstractions.VirtualPath.VirtualPathManager.GetCacheDependency(String virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) +449

    I got round this by creating the directories '/bundles/plugins' but then when I run the site the link to the minified JS does not work.



  • Any one else who is interested in using Bundling in Sitefinity you need to include an extra line to stop Sitefinity interfering:

    protected void Application_Start(object sender, EventArgs e)     
    BundleConfig.RegisterBundles(BundleTable.Bundles);     BundleTable.VirtualPathProvider =  System.Web.Hosting.HostingEnvironment.VirtualPathProvider;    
    BundleTable.EnableOptimizations = true; 

    Thanks to Yavor for his help.
  • Hi Gunnar,

    I'm not able to find the code right now. It should be as simple as adding the literal control to the Page.Controls collection:

    Page.Controls.Add(new Literal() Text = scripts );

    Let me know if that works for you?

    Best regards,

  • Dear Daniel, I am missing the code in your post for the method to add script reference before end of body. Would it be possible for you to reveal the code here?


  • Nope. That does not work.

    Server Error in '/' Application.


    The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases



    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

    Source Error:

    Line 77: var scripts = Scripts.Render(relativeScriptPath).ToHtmlString(); Line 78: Line 79: this.Page.Controls.Add(new Literal() Text = scripts ); Line 80: Line 81:

  • Hi Gunnar,

    OK, probably you need to add this to the Page.Form.Controls collection. Maybe you can try that?

    Maybe something like this:

    Page.Form.Controls.AddAt(Page.Form.Controls.Count, new Literal() Text = scripts1 );