Bundling and Minification in asp.net mvc
Microsoft Net Framework

Bundling and Minification in asp.net mvc

a) Bundling allow to load more than one file in one request
b) Minification remove unnecessary white space and comments and shortening variable names to one character.


in App_Start\BundleConfig.cs add following function

public static void RegisterBundles(BundleCollection bundles)
{   

 bundles.Add(new StyleBundle("~/Common/css/min/tcgrid").Include("~/Common/css/tcgrid.css"));


ScriptBundle scriptBndl = new ScriptBundle("~/bundles/bootstrap");         
    bundles.Add(new ScriptBundle("~/bundles/scripts").IncludeDirectory("~/Scripts/","*.js",true));
BundleTable.EnableOptimizations = true;
}


 in Global.asax on  Application_Start event

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

The script bundles can be included using static Scripts class. Use Scripts.Render() method to include specified script bundle at runtime.

in head of page we can as

<head>
@Scripts.Render("~/bundles/bootstrap")
</head>


Note!! in web.config dont forget to set debug=false  below

  <system.web>
    <!--<sessionState mode="InProc" timeout="60"></sessionState> -->
    <!--Session will be time out after 60 minutes -->
    <sessionState mode="InProc" cookieless="false" timeout="60"/>
    <compilation debug="true" targetFramework="4.5"/>

 

Ref:  https://docs.microsoft.com/en-us/aspnet/mvc/overview/performance/bundling-and-minification
thanks,
above are the steps how we can use bundling and minification in asp.net mvc

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM