Nov 29, 2012

Bundling and Minification in MVC 4 And Website Then Project Solution

Bundling and Minification in MVC 4 

Bundling: It’s a simple logical group of files that could be referenced by unique name and being loaded with one HTTP requestor.

Minification: It’s a process of removing unnecessary whitespace, line break and comments from code to reduce its size thereby improving load times.



Project Solution:

Tools->Library Package manager->Package Manager console click
bottom below use
PM>Install-Package Microsoft.Web.Optimization –Pre

Support in Bin Files:

Microsoft.system.Web.Optimazation
Microsoft.Web.infrastructure
WebGrease

Using Global.asax using Register bundles with Application_Start:


  void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Bundle ins1 = new Bundle("~/Htpage");
        ins1.Include("~/Scripts/jquery-1.4.1-vsdoc.js");
        ins1.Include("~/Scripts/jquery-1.4.1.js");
        ins1.Include("~/Scripts/jquery-1.4.1-min.js");
    }
 
Using Application page using unique name js file using Htpage/Scripts
 
<head runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <%: System.Web.Optimization.Scripts.Render("~/Htpage/Scripts" )%>
     
</head>
 
Website :
 
1.    Add Dll File Microst.Web.optimazation.dll in bin folder.
2.    then global.asax  file to add  global.asax.cs file to needed So Add Global.cs
How can add Global.cs file 
1.    App_data to added the new Class file ,File Name is Global.cs
2.    Add Import package name “Using System.web.optimazation”
3.    class name to add “public class Global:System.Web.HttpApplication”
4.    Add the Application_Start function below item .
5.    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Bundle ins1 = new Bundle("~/Htpage");
           ins1.Include("~/Scripts/jquery-1.4.1-vsdoc.js");
           ins1.Include("~/Scripts/jquery-1.4.1.js");
           ins1.Include("~/Scripts/jquery-1.4.1-min.js");
    }
 
6. Using Application page using unique name js file usingHtpage/Scripts
 
<head runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <%: System.Web.Optimization.Scripts.Render("~/Htpage/Scripts" )%>
     
</head>
 Before And After checking page script size in Firebox 
 
 
 
 
 






 

MVC 4 Features



Ø       Bundling & Minification
o        Instead of <script href=”/Scripts/main.js” you add <script href=”/Scripts”. This will combined all scripts in the folder in to one HTTP request and minify it all. It can also add a unique script to the end causing the bundle to cache for a year, then if any files in the bundle change this will update causing a refresh of the css/js.
o        Coffeesciprt & Less support when registering custom bundles allowing built in transformation from asp.net on the server load of these types
Ø       Extension to razor scripts
o        You can now use the tilde (~) in any html attribute and razor will automatically resolve to the correct URL
o        Conditional attributes eg <div class=”@myclass”>Content</div>. If variable myclass is null or empty the class attribute will not render, works on any attribute making cleaner code
Ø       WebAPI
o        Allows the building of HTTP APIs that automatically can serialize to the format requested such as xml/json
o        Support oData so that if the returned object support iQueryable parameters can be added to the return to subsets (paging, top n etc)
o        Allows hosting of WebAPI either in an area web, console, service, windows 8
o        Allows results to easily return custom HTTP response codes and location header for querying data
Ø       Mobile development
o        Supports adaptive rendering using media queries
o         Allows creation of different mobile specific views
o        Supports mobile templating engines such as jQuery mobile
Ø       Real time communication
o        Uses SignalR
o        Automatically negotiates the transport (essential using the most optimised form of transportation)
o        .NET way of doing what everyone is using node.js for
o        Allows the server to call JavaScript, e.g pushes a call to the browser thus executing some JavaScript
Ø       Async keyword
o        As of VS11 support Async keyword to allow more performance and scalability

Recent Posts
Popular Articles

 

© 2013 MUNISH ORACLE DBA& .Net Developer. All rights resevered. Designed by Templateism

Back To Top