引用 System.Web.Optimization
使用 NuGet 安装 Microsoft.AspNet.Web.Optimization
App_Start 添加 BundleConfig.cs
using System.Web;
using System.Web.Optimization;
namespace WebApp
{
public class BundleConfig
{
// For more information on bundling, visit https://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
#region == 首页 ==
// js
bundles.Add(new ScriptBundle("~/js/main").Include(
"~/scripts/jquery/jquery.js",
"~/Scripts/mvcUtils.js",
"~/Scripts/menu/pubMenu.js",
"~/Scripts/audio.js"
));
// css
bundles.Add(new StyleBundle("~/css/main").Include(
"~/css/theme/less/main.css"));
#endregion
}
}
}
Global.asax.cs 注册
protected void Application_Start()
{
//js,css
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
页面调用
<head>
<meta charset="utf-8" />
<title>任务协同</title>
@using System.Web.Optimization
@Styles.Render("~/css/main")
@Scripts.Render("~/js/main")
</head>
发布时,设置 Web.config: debug="false",启用压缩与合并。
<system.web>
<!--是否合并压缩 js,css-->
<compilation debug="false" targetFramework="4.5">
</system.web>
效果 index.html
自动压缩,合并。并且生成版本号,解决浏览器缓存问题。
<head>
<meta charset="utf-8">
<title>任务协同</title>
<link href="/css/main?v=6xh0yiaxKbXhcRYgih6n26-4oDT0A5u_v4A07zfnBWo1" rel="stylesheet">
<script src="/js/main?v=9S5pFbFCQDjQTpoGW7YzC8VS_zG7Bd1enQAkaXXpKnw1"></script>
</head>
网友评论