Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 02, 2014 @ 18:12
    Jeroen Breuer
    0

    Web Optimization Framework not rendering correctly

    Hello,

    This question isn't completly Umbraco related, but I rather ask my questions here than on SO. I'm using Bundling and Minification with the Web Optimization Framework and somehow it doesn't render. It's an Umbraco 4.7.2 website and I register the bundles at the same place I register events. Here is a piece of the code:

    using System.Web.Optimization;
    public class SiteApplicationBase : ApplicationBase
    {
        public SiteApplicationBase()
    { RegisterStyles(BundleTable.Bundles); RegisterJavascript(BundleTable.Bundles); Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish); } private static void RegisterStyles(BundleCollection bundles) { //Add all style files. These will be bundled and minified. //After everything has been bundled (which only happens when debug=false in the web.config) check if there are no errors in the bundled file. Not all files can be bundled. bundles.Add(new StyleBundle("~/bundle/styles.css").Include( "~/css/main.css" ) ); } private static void RegisterJavascript(BundleCollection bundles) { //Add all javascript files. These will be bundled and minified. //After everything has been bundled (which only happens when debug=false in the web.config) check if there are no errors in the bundled file. Not all files can be bundled. bundles.Add(new ScriptBundle("~/bundle/javascript.js").Include( "~/scripts/script1.js", "~/scripts/script2.js" ) ); }

    After that I try to show the bundle with Razor:

    @using System.Web.Optimization
    
    @Scripts.Render("~/bundle/javascript.js")

    So I expect that the HTML rendered would be this:

    //Not bundled:
    <script src="/scripts/script1.js"></script>
    <script src="/scripts/script2.js"></script>
    
    //Bundled:
    <script src="/bundle/javascript.js?v=EyZOk_ock7ySHjK9_YsbSjFcqW0fh-_yJ8-SJh0rbX01"></script>
    

    But instead it outputs this:

    <script src="/bundle/javascript.js"></script>

    It doesn't matter if bundling is enabled or disabled it always points to the path I'm using in Razor. Anyone know what could be the problem? I have it working on other websites where I create the bundles in OnApplicationStarted with the IApplicationEventHandler interface, but Umbraco 4.7.2 doesn't have that yet. 

    Jeroen

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jan 02, 2014 @ 21:59
    Andy Butland
    0

    It might be because you are using .js and .css in your actual bundle names?  Not sure if that confuses things but you might try calling it just "~/bundle/javascript"

    Worth a try anyway... that's the only thing that jumped out at me as different from how I've used it.

    Andy

  • Mads Krohn 211 posts 504 karma points c-trib
    Jan 02, 2014 @ 23:06
    Mads Krohn
    0

    +1 to Andy's suggestion.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 03, 2014 @ 11:41
    Jeroen Breuer
    0

    You can read here why I added .js and .css: https://twitter.com/j_breuer/status/367198864820158464

    I tried it without the extension, but it's the same problem. Maybe I need to register them somewhere else instead of ApplicationBase.

    Jeroen

  • Mads Krohn 211 posts 504 karma points c-trib
    Jan 03, 2014 @ 12:08
    Mads Krohn
    0

    Ok, I've never had issues with Umbraco and bundle names without .js/.css though, so can't cooperate on that.

    Originally I thought that perhaps the .js/.css extensions in the bundle names would make the bundle handlers ignore them. But if it doesn't work without extensions as well, then it sounds like the handlers aren't wired up at all.

    As you said yourself, perhaps you need to register the bundle config somewhere else than ApplicationBase.

    Let us know here if you solve it yourself, otherwise I'll have a look at it later.

    Cheers

  • Mads Krohn 211 posts 504 karma points c-trib
    Jan 04, 2014 @ 00:07
    Mads Krohn
    0

    You could try to register the bundle config on Application_Start as so:

    public class SiteGlobal : Global
    {
        protected override void Application_Start(object sender, EventArgs e)
        {
            // Code goes here
        }
    }
    

    Let me know if that helps at all :)

    Cheers

Please Sign in or register to post replies

Write your reply to:

Draft