最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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.

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.

Share Improve this question asked Jan 8, 2014 at 8:29 SunilSunil 1282 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

Yes, you can use bundling in ASP 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") %>

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./jquery-1.9.1.js";
 var jQueryUICdnPath = "http://code.jquery./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.

  1. Add dll file WebGrease.dll in Reference
  2. Add Bellow code for js and css in global.asax

    dynamic solutioncss = new System.Web.Optimization.StyleBundle("~/bundles/solutionDetailCSSBundle"); solutioncss.Include("~/Style.css", new CssRewriteUrlTransform()); solutioncss.Include("~/incs/highslide/highslide.css", new CssRewriteUrlTransform()); solutioncss.Transforms.Add(new CssMinify()); System.Web.Optimization.BundleTable.Bundles.Add(solutioncss);

    dynamic HeaderLinkBundle = new System.Web.Optimization.ScriptBundle("~/bundles/HeaderLinkBundle"); HeaderLinkBundle.Include("~/js/jquery.js"); HeaderLinkBundle.Include("~/js/headerlink.js"); HeaderLinkBundle.Transforms.Add(new JsMinify()); System.Web.Optimization.BundleTable.Bundles.Add(HeaderLinkBundle);

    System.Web.Optimization.BundleTable.EnableOptimizations = true;

  3. Add this bundle of css and js to aspx page.

发布评论

评论列表(0)

  1. 暂无评论