Sampath
Sampath

Reputation: 65978

Minification JS file on each and every *.cshtml page

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> 

Upvotes: 1

Views: 991

Answers (2)

Skull
Skull

Reputation: 1262

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

Upvotes: 0

user3559349
user3559349

Reputation:

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")

Upvotes: 2

Related Questions