1

The app I'm working on has been developed by using ASP.net MVC and AngularJs. It's not a SPA app.I need to do bundling and minification of my JS files.I know how to do it on "Layout page".For that I have used this Bundling and Minification tutorial.

But My question is how can I Minification my JS file on each and every *.cshtml page ? And I need that JS file as unminified when I do the debug.Can I achieve that ? Any help would be highly appreciated.

Here is my "index.cshtml" page with JS file resides on bottom of the page.I would like to do Minification this js file.

//removed html code for clarity
<script type="text/javascript" src="/Resources/js/controllers/MyTestController.js"> </script> 
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • The tutorial shows you how to create a `ScriptBundle` and include it on a page. What part don't you understand? (and if you want it on every page, why not just put in the layout page so you only need to include it once) –  Dec 04 '14 at 05:52
  • @StephenMuecke No.When I put those angular controller JS files on layout page ,it doesn't work.Need to load those files on each and every *.cshtml page.Otherwise It won't bind the controller.I have put all other common js on layout page and works fine.The problem is how to do minification on page level js files ? – Sampath Dec 04 '14 at 06:00
  • Its no different. Just create a new `ScriptBundle` that includes that file, then add `@Scripts.Render("~/bundles/yourFile")` in the pages –  Dec 04 '14 at 06:04
  • @StephenMuecke Thanks for suggestion.Can you put that as an answer ? Then I can close this thread. – Sampath Dec 04 '14 at 06:06

2 Answers2

2

Add a new ScriptBundle in BundleConfig.cs

bundles.Add(new ScriptBundle("~/bundles/yourFile").Include(
  "..." // your file(s)
));

and in you view

@Scripts.Render("~/bundles/yourFile")
0

1.Use Nuget to install the package: Install-Package Microsoft.AspNet.Web.Optimization
2.Create a BundleConfig Class and define your bundles:

using System.Web.Optimization;
public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/js").Include(
                  "~/Scripts/*.js"));
        bundles.Add(new StyleBundle("~/bundles/css").Include(
                   "~/Styles/*.css")); 
    } 
}

3.Register the BundleConfig class within the application start in the global.asax

void Application_Start(object sender, EventArgs e)
{
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

4.reference the bundles in your HTML document.
5.Enable bundling by disabling debug mode.
reference source: Bundling and minification without ASP.NET MVC

Community
  • 1
  • 1
Skull
  • 1,204
  • 16
  • 28