Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Josefine 5 posts 77 karma points
    Mar 16, 2019 @ 11:10
    Josefine
    1

    OnApplicationStarted Umbraco 8

    How do I register bundle config on application started in Umbraco 8?

    Before in Umbraco 7 I used this:

    public class MvcApplication : Umbraco.Web.UmbracoApplication
    {
        protected override void OnApplicationStarted(object sender, EventArgs e)
        {
            base.OnApplicationStarted(sender, e);
    
            AreaRegistration.RegisterAllAreas();
            BundleConfig.RegisterBundles(BundleTable.Bundles);
    
        }
    }
    
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new StyleBundle("~/bundles/site.css").Include(
            "~/css/style.css"
            ));
        }
    }
    

    Thanks,

    Josefine

  • Sebastiaan Janssen 4893 posts 14611 karma points MVP admin hq
    Mar 18, 2019 @ 07:23
    Sebastiaan Janssen
    5

    While I'm not too familiar with Bundles, have a look at the documentation for attaching to events, that happens during the Initialization of Umbraco so hopefully works for you:

    https://our.umbraco.com/documentation/Implementation/Composing/#example---creating-a-component-to-listen-for-contentservicesaving-events

    Example code could look a bit like:

    using System.Web.Mvc;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    
    namespace MyProject.Components
    {
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class MyComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<MyComponent>();
            }
        }
    
        public class MyComponent : IComponent
        {
            public MyComponent()
            {
    
            }
    
            public void Initialize()
            {
                AreaRegistration.RegisterAllAreas();
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
    
            public class BundleConfig
            {
                public static void RegisterBundles(BundleCollection bundles)
                {
                    bundles.Add(new StyleBundle("~/bundles/site.css").Include(
                        "~/css/style.css"
                    ));
                }
            }
    
            public void Terminate()
            {
            }
        }
    }
    
  • Josefine 5 posts 77 karma points
    Mar 24, 2019 @ 10:37
    Josefine
    0

    Thanks a lot! :)

    Regards Josefine

  • pbl_dk 132 posts 480 karma points
    Jul 22, 2020 @ 20:41
    pbl_dk
    0

    Just delete MyComponent, else there will be a return null error:

    using NAMESPACE.App_Start;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Optimization;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    
    namespace   NAMESPACE.Components
    {
    
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class MyComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<BundleComponent>();
            }
        }
    
        public class BundleComponent : IComponent
        {
    
      public void Initialize()
      {
       BundleConfig.RegisterBundles(BundleTable.Bundles);
      }
    
      public void Terminate()
      {
      }
     }
    }
    

    Also place bundleconfig in APP START

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Optimization;
    
    namespace NAMESPACE.App_Start
    {
    
            public class BundleConfig
            {
                public static void RegisterBundles(BundleCollection bundles)
                {
                bundles.Add(new StyleBundle("~/bundles/css").Include(
                    "~/css/screen.css"));
    
                bundles.Add(new ScriptBundle("~/bundles/app").Include(
                                    "~/_publish/scripts/app.js"));          
    
    
            }
        }
    }
    

    Remember webconfig:

     <add key="Umbraco.Core.ReservedPaths" value="~/bundles" />
    
Please Sign in or register to post replies

Write your reply to:

Draft