1.在Web.config中配置标签:MVC4,Web,ASP,bundles,js,lp,WEBFORM,sem,css From: https://blog.51cto.com/u_14062833/6087818
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
2.在Global.asax中注册自定义的捆绑类
protected void Application_Start(object sender, EventArgs e)
{
//注册捆绑压缩JS与CSS
BundleTable.EnableOptimizations = true;//是否开启合并
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
3.新建一个自定义类叫BundleConfig
using System.Web;
using System.Web.Optimization;
namespace MetenSite.Web
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
// 在/sem/lp/js 下的页面底部的js
bundles.Add(new ScriptBundle("~/bundles/sem/lp/bundlejs").Include(
"~/sem/lp/js/com.js",
"~/sem/lp/js/js-video.js",
"~/sem/lp/js/leftnav.js",
"~/sem/lp/js/phonemd.js",
"~/sem/lp/js/regsiter.js",
"~/sem/lp/js/jquery.cookie.js"));
// 在/sem/lp/css下的页面头部的CSS
bundles.Add(new StyleBundle("~/bundles/sem/lp/css/bundletopcss").Include(
"~/sem/lp/css/com.css",
"~/sem/lp/css/register.css"));
// /sem/lp/hotel.aspx css
bundles.Add(new StyleBundle("~/bundles/sem/lp/hotelcss").Include("~/sem/lp/css/hotel.css"));
// 在/sem/lp/css下的页面底部的CSS
//bundles.Add(new StyleBundle("~/bundles/sem/lp/css/bundlebottomcss").Include(
// "~/sem/lp/css/header_footer.css",
// "~/sem/lp/css/hoteldown.css",
// "~/sem/lp/css/inquire.css",
// "~/sem/lp/css/interview.css",
// "~/sem/lp/css/listening.css",
// "~/sem/lp/css/online_talk.css",
// "~/sem/lp/css/online_website.css",
// "~/sem/lp/css/onlineStudy.css",
// "~/sem/lp/css/onlineTest.css",
// "~/sem/lp/css/sentences.css",
// "~/sem/lp/css/tour.css",
// "~/sem/lp/css/videoList.css",
// "~/sem/lp/css/voa.css",
// "~/sem/lp/css/zero.css",
// "~/sem/lp/css/zero_course.css",
// "~/sem/lp/css/zero_talk.css",
// "~/sem/lp/css/zeroTalk.css"));
}
}
}
4.在页面上引用WEBFORM下
CSS的引用
<%: System.Web.Optimization.Styles.Render("~/bundles/sem/lp/hotelcss")%>
JS的引用
<%: System.Web.Optimization.Scripts.Render("~/bundles/sem/lp/bundlejs")%>
MVC 3.0及以上
CSS的引用
@System.Web.Optimization.Styles.Render("~/bundles/sem/lp/hotelcss");
JS的引用
@System.Web.Optimization.Scripts.Render("~/bundles/sem/lp/bundlejs");