问题
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.
回答1:
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") %>
回答2:
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.
回答3:
- Add dll file WebGrease.dll in Reference
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;
Add this bundle of css and js to aspx page.
来源:https://stackoverflow.com/questions/20990391/can-i-use-bundling-bundle-config-in-net-4-asp-net-web-application