介绍
<形式类=" form-horizontal ">
本篇文章为大家展示了包如何在ASP。净MVC中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
ASP。净MVC中包是用于打包捆绑资源的(一般是css和js),它是在全局文件Global.asax.cs中注册包,而注册的具体实现默认是在App_Start文件夹的BundleConfig。cs中
public class MvcApplication : System.Web.HttpApplication { ,protected void  Application_Start () ,{ AreaRegistration.RegisterAllAreas才能(); FilterConfig.RegisterGlobalFilters才能(GlobalFilters.Filters); RouteConfig.RegisterRoutes才能(RouteTable.Routes); BundleConfig.RegisterBundles才能(BundleTable.Bundles); ,} }
BundleConfig.RegisterBundles (BundleTable.Bundles);在应用程序启用时注册包
public class BundleConfig { ,//有关绑定的详细信息,请访问,http://go.microsoft.com/fwlink/?LinkId=301862 ,public static  void RegisterBundles (BundleCollection 包) ,{ bundles.Add才能(new ScriptBundle(“~/包/jquery")其中包括( ,,,,,~/脚本/jquery - {version} .js")); bundles.Add才能(new ScriptBundle(“~/包/jqueryval")其中包括( ,,,,,~/脚本/jquery.validate *“));//,才能使用要用于开发和学习的,Modernizr 的开发版本。然后,当你做的好//,才能生产准备时,请使用,http://modernizr.com 上的生成工具来仅选择所需的测试。 bundles.Add才能(new ScriptBundle(“~/包/modernizr")其中包括( ,,,,,~/脚本/modernizr - *“)); bundles.Add才能(new ScriptBundle(“~/包/bootstrap")其中包括( ,,,,,~/脚本/bootstrap.js" ,,,,,~/脚本/respond.js")); bundles.Add才能(new StyleBundle(“~/内容/css")其中包括( ,,,,,~/内容/bootstrap.css" ,,,,,~/内容/site.css")); ,} }
为了便于说明,这里在HomeController下新建一个行动,如下:
public ActionResult BundleTest () { ,return 视图(); }
这里以使用引导为例,在视图中使用@Styles.Render和@Scripts.Render()()引入css和js,参数是在BundleConfig注册的名称
@ {=,Layout 零; } & lt; ! DOCTYPE html> & lt; html> & lt; head> ,& lt; meta name=皏iewport",内容=?device-width"宽度,/比; ,& lt; title> BundleTest ,@Styles.Render(“~/内容/css") & lt;/head> & lt; body> , ,@Scripts.Render(“~/包/jquery",,“~/包/bootstrap") & lt;/body> & lt;/html>
浏览页面,查看源代码,可以看到:
bundles.Add (new StyleBundle(“~/内容/css")其中包括( ,,,,,,~/内容/bootstrap.css" ,,,,,,~/内容/site.css"));
由于在BundleConfig.cs中注册上面的Bundle, @Styles.Render(“~/内容/css")渲染时是引入~/内容/bootstrap.css和~/内容/网站。css, js的渲染同理
为了验证是否真正引入了引导的css与js资源,这里添加了一些简单的引导示例代码,如下:
@ {=,Layout 零; } & lt; ! DOCTYPE html> & lt; html> & lt; head> ,& lt; meta name=皏iewport",内容=?device-width"宽度,/比; ,& lt; title> BundleTest ,@Styles.Render(“~/内容/css") & lt;/head> & lt; body> ,& lt; div 类=癱ontainer"比; & lt;才能div 类=癶eader clearfix"比; ,,& lt; nav> ,,,& lt; ul 类=皀av nav-pills pull-right"比; ,,,,& lt; li 角色=皃resentation",类=癮ctive"祝辞& lt; https://www.yisu.com/zixun/a href=" # ">首页> 李> <李的作用="表示"> 关于我们> 李> <李的作用="表示"> 联系我们> 李>