Fork me on GitHub

【记录】ASP.NET MVC RegisterBundles

1. Install the package from nuget

Install-Package Microsoft.AspNet.Web.Optimization

2,BundleConfig 配置

using System.Web.Optimization;

namespace CNBlogsMsg.Web
{
    public class BundleConfig
    {
        // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/demo_js.js").Include(
                        "~/scripts/demo_js_1.js")
                        );

            bundles.Add(new StyleBundle("~/bundles/demo_css.css").Include(
                        "~/css/demo_css_2.css",
                        "~/css/demo_css_2.css")
                        );
        }
    }
}

3. Global.asax-Application_Start 配置

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);//添加注册配置
        }

4. Views 配置

@using System.Web.Optimization;
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@Page.Title</title>
    @Styles.Render("~/bundles/demo_css.css")
    @Scripts.Render("~/bundles/demo_js.js")
</head>
<body>
    <div>
        demo
    </div>
</body>
</html>

5. web.config 配置

  <!--禁用调试模式,否则没有 Hash 值生成-->
  <system.web>
    <compilation debug="false" targetFramework="4.5.1" />
  </system.web>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

6. 效果

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Title</title>
    <link href="/bundles/demo_css.css?v=I3rpG68fm1kRMzWXmKkubZQkNLGHPBbWwHrdU7nUhtw1" rel="stylesheet"/>
    <_script src="/bundles/demo_js.js?v=gf-dTESU0iXHXV4Jp47ItNHcKFXY6h0iN-ExF9Ih0Mo1"></_script>
</head>
<body>
    <div>
        demo
    </div>
</body>
</html>

7. 参考资源

posted @ 2014-09-04 19:08  田园里的蟋蟀  阅读(2056)  评论(0编辑  收藏  举报