+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Anyone using the new Bundling Optimization from .NET 4.5?

Anyone using the new Bundling Optimization from .NET 4.5?

8 posts, 0 answered
  1. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    11 Sep 2012
    Link to this post
    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") %>
    </asp:PlaceHolder
    <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?

  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    16 Sep 2012
    Link to this post
    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
                AddCssReference("~/Assets/Styles/css");
     
                // Add javascript files
                AddJsScriptReferenceBeforeBodyEndTag("~/Assets/Scripts/js");
     
                // 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)
                    return;
     
                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.

    Regards,
    Daniel
  3. Stuart
    Stuart avatar
    18 posts
    Registered:
    28 Jul 2011
    25 Oct 2013 in reply to Stacey
    Link to this post
    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.

    Cheers,

    Stuart





  4. Stuart
    Stuart avatar
    18 posts
    Registered:
    28 Jul 2011
    05 Nov 2013
    Link to this post
    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.
  5. Gunnar
    Gunnar avatar
    10 posts
    Registered:
    30 Sep 2008
    19 May in reply to Daniel Plomp
    Link to this post

    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?

    Rgds/Gunnar

  6. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    19 May
    Link to this post

    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,
    Daniel

  7. Gunnar
    Gunnar avatar
    10 posts
    Registered:
    30 Sep 2008
    27 May in reply to Daniel Plomp
    Link to this post

    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:

  8. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    27 May in reply to Gunnar
    Link to this post

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

    Best,
    Daniel

8 posts, 0 answered