MVC.Net:添加第三方类库的bundles引用

以jQuery Mobile为例。当我们用nuget添加jQuery Mobile的package后,需要在项目中引用jQuery Mobile。传统的做法是在html页面上添加
<link href="/Content/jquery.mobile-1.4.2.css" rel="stylesheet"/>
<script src="/Scripts/jquery.mobile-1.4.2.js"></script>

不过在MVC.Net里我们可以用Scripts.Render和Styles.Render来实现。
1.  在_layout.cshtml页面或是其它特定页面上,添加
@Scripts.Render("~/bundles/jquery.mobile")
@Styles.Render("~/Content/css")
2.  编辑BundleConfig类,添加

bundles.Add(new ScriptBundle("~/bundles/jquery.mobile").Include("~/Scripts/jquery.mobile-{version}.js"));

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/jquery.mobile-1.4.2.css"));

 

保存并运行程序,你就能开始使用jQuery Mobile。这中做法比传统做法的优点在于调用的第三方类库的版本能够统一在BundleConfig类中管理,从而减少升级版本带来的影响。

原文地址:https://www.cnblogs.com/ilovewindy/p/3745471.html