Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use Bundling [Bundle.Config] in .net 4 Asp.Net web Application

I have my application in .Net framework 4.It is a Asp.Net Web Application.i need to use Bundle.Config in order to use Bundling feature.

I have read many documents saying that, it is feature in .Net framework 4.5 and that to in Asp.Net MVC Application.

I need to make a bundle for Scripts in aspx pages. Can i include Bundle.Config in my file so that Bundling works.

like image 267
Sunil Avatar asked Dec 06 '22 03:12

Sunil


2 Answers

Yes, you can use bundling in ASP.net 4. Use Nuget Package Manager to install Microsoft ASP.Net Web Optimization Framework to your project. Then in global.asax register the bundles in Application_Start method. something like this -

    var jqueryBundle = new ScriptBundle("~/Scripts/jquery");
    jqueryBundle.Include(new string[] { 
        "~/Scripts/jquery-1.8.3.js",
        "~/Scripts/jquery-ui-1.9.1.custom.min.js",
        "~/Scripts/jquery-ui-timepicker-addon.js",
        "~/Scripts/jquery.validate.js",
        "~/Scripts/jquery.validate-additional-methods.js"
    });

    BundleTable.Bundles.Add(jqueryBundle);

Then in your aspx page or masterpage call the bundle-

    <%= System.Web.Optimization.Scripts.Render("~/Scripts/jquery") %>
like image 135
th1rdey3 Avatar answered Dec 30 '22 09:12

th1rdey3


Over couple trial and Reading Bundling i found the solution

Install Web Optimizer framework from NuGet package Manager for the solution include System.Web.Optimization in following file, even in Apsx file.

in Application_StartUp () :

 var bundles = BundleTable.Bundles;
 bundles.UseCdn = true;   //enable CDN support
 var jqueryCdnPath = "http://code.jquery.com/jquery-1.9.1.js";
 var jQueryUICdnPath = "http://code.jquery.com/ui/1.10.3/jquery-ui.js";
 bundles.Add(new ScriptBundle("~/bundles/jquery",jqueryCdnPath)); 
 bundles.Add(new ScriptBundle("~/bundles/jqueryui", jQueryUICdnPath)); 

In Aspx page :

  <script src="<%=BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryui")%>" type="text/javascript"></script>
  <script src="<%=BundleTable.Bundles.ResolveBundleUrl("~/bundles/jquery")%>" type="text/javascript"></script>

~/bundles/jqueryui : for UI java script ~/bundles/jquery : for functionality java script.

like image 34
Sunil Avatar answered Dec 30 '22 09:12

Sunil