How to automatically minify CSS/Javascript in ASP.NET MVC5

Asked

Viewed 316 times

3

I use Visual studio 2017, my application runs on ASP.NET MVC5 (Razor), there is a way to minify all the files I choose at the time of "Publish" ?

1 answer

3


You have the Framework Bundle. Inside your printer App_Start create a class BundleConfig.cs and add:

public static void RegisterBundles(BundleCollection bundles)
{
    //para javascript, você pode fazer vários includes para o mesmo arquivo "scripts"        
    bundles.Add(new ScriptBundle("~/bundles/scripts").Include("~/seuJs.js"));
    //para Css
    bundles.Add(new StyleBundle("~/Content/css").Include("~/seuCss.css"));

    //EnableOptimizations força a minificação mesmo em desenvolvimento, serve para fazer teste se esta minificado corretamente.
    //BundleTable.EnableOptimizations = true;
}

Then just register it in your Application_Start:

BundleConfig.RegisterBundles(BundleTable.Bundles);

In your . cshtml you need to have javascript or css rendered:

<head>
    @Styles.Render("~/Content/css")
</head>
<body>
   @Scripts.Render("~/bundles/scripts")
</body>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.