Script Bundling in WebForms with Virtual Directori

2019-07-16 11:18发布

问题:

We have a separate "Framework" project for shared controls, javascript, etc. that is used by this and other projects. The Framework project is included in our solution, and we made a virtual directory for it with IIS so we can still make relative links to files with ~/Framework.

So, we want to begin using the new Web Optimization bundles for scripts and styles. Everything is working fine, but we are having issues when attempting to use bundling with JS files in the virtual directory. When debug="false" is set, the files are correctly bundled & minified. When debug="true" is set it does not include any of the files!

Clearly it has access to the files since it can bundle & minify them together. Is there a way to make this work?


Here's what I have in App_Start/BundleConfig.vb

    Public Shared Sub RegisterBundles(bundles As BundleCollection)
        Dim mainBundle = New ScriptBundle("~/bundles/main")
        mainBundle.Orderer = New AsIsBundleOrderer()
        mainBundle.Include("~/Framework/Javascript/Main/Library.jQuery.js")
        mainBundle.Include("~/Framework/Javascript/Main/Library.jQuery.ui.js")
        'snip....
        mainBundle.Include("~/Framework/Javascript/Main/CP.Base.js")

        bundles.Add(mainBundle)
    End Sub

Here's what I have in my MasterPage:

    <%: System.Web.Optimization.Scripts.Render("~/bundles/main") %>

回答1:

create a class under App_Code folder in your project and add this code in to the class

Public Shared Sub RegisterBundles(bundles As BundleCollection)
    Dim mainBundle = New ScriptBundle("~/bundles/main")
    mainBundle.Orderer = New DefaultBundleOrderer()
    mainBundle.Include("~/HTML5Components/adc.js")
    bundles.Add(mainBundle)
End Sub

after that open Global.asax

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    BundleConfig.RegisterBundles(BundleTable.Bundles)         
End Sub