admin管理员组

文章数量:1322838

I'm setting up my MVC4 app to use the built-in ASP.Net bundling (converting from Squishit). I've set up the BundleConfig class, and things all work fine, but for cases where a Razor file only has a reference to one JavaScript file, I don't see the need to involve that config file. I'd rather just add the script to the BundleTable right in my Razor 'Scripts' section, and Render it right there. That way I can do a release to production quite easily for small changes if I have to rather than change the BundleConfig file. This is what I have:

@section Scripts
{
    @{   
        const string bundle = "~/bundles/{myVirtualBundlePath}";
        BundleTable.Bundles.Add(new ScriptBundle(bundle)
            .Include("{myActualJavaScriptFilePath}"));
        Scripts.Render(bundle);
    }
}

and while this builds ok, the reference to that bundled file never gets output to HTML.

Do I need to get a reference to the particular BundleCollection that the BundleConfig uses?

I'm setting up my MVC4 app to use the built-in ASP.Net bundling (converting from Squishit). I've set up the BundleConfig class, and things all work fine, but for cases where a Razor file only has a reference to one JavaScript file, I don't see the need to involve that config file. I'd rather just add the script to the BundleTable right in my Razor 'Scripts' section, and Render it right there. That way I can do a release to production quite easily for small changes if I have to rather than change the BundleConfig file. This is what I have:

@section Scripts
{
    @{   
        const string bundle = "~/bundles/{myVirtualBundlePath}";
        BundleTable.Bundles.Add(new ScriptBundle(bundle)
            .Include("{myActualJavaScriptFilePath}"));
        Scripts.Render(bundle);
    }
}

and while this builds ok, the reference to that bundled file never gets output to HTML.

Do I need to get a reference to the particular BundleCollection that the BundleConfig uses?

Share Improve this question asked Jan 16, 2013 at 0:34 Ralph LavelleRalph Lavelle 5,7694 gold badges39 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Only difference between mine (that works) is that I add the bundle outside of the script block. If this doesn't work make sure it does work in the main bundle start-up, it could be some other issue.

@{ BundleTable.Bundles.Add(new ScriptBundle("~/Scripts/Plugins/highcharts")
                           .Include("~/Scripts/Plugins/highcharts.js")); }
@section Scripts
{    
    @Scripts.Render("~/Scripts/Plugins/highcharts")
    @Scripts.Render("~/Scripts/Views/Charts/transportDelayCharts")
}

本文标签: javascriptIn MVC4how do I create a script bundle in a Razor fileStack Overflow