欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ASP.NET MVC RegisterBundles

程序员文章站 2022-07-08 14:39:31
...

###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>


本文转自田园里的蟋蟀博客园博客,原文链接:http://www.cnblogs.com/xishuai/p/asp-net-mvc-register-rundles.html,如需转载请自行联系原作者