第24章 捆绑包

----------------------------------------------

说明:此篇为当前系列的最后一篇,关于面向移动设备、WEB API 等内容暂时不学。在后面将开发实战项目。

----------------------------------------------

一、脚本和样式捆绑包

  位置  :/App_Start/BundleConfig.cs

  命名空间:System.Web.Optimization

    RegisterBundles静态方法会在应用程序第一次启动时,通过Global.asax中Application_Start方法调用

   BundleConfig.RegisterBundles(BundleTable.Bundles)

    创建新的捆绑包时,就是创建StyleBundle或ScriptBundle类的实例,通过构造器参数引用捆绑包路径。

  为了防止冲突,最安全的方式是以~/bundles或~/Content作为起始路径。

 

  定制捆绑包的配置:

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/*.css"));

            bundles.Add(new ScriptBundle("~/bundles/clientfeaturesscripts").Include(
                "~/Scripts/jquery-{version}.js",
                "~/Scripts/jquery.validate.js",
                "~/Scripts/jquery.validate.unobtrusive.js",
                "~/Scripts/jquery.unobtrusive-ajax.js"
                ));

提示:web.config中complilation,true表示常规版,false表示压缩版

注   :MVC 框架处理捆绑包中的{version}时,会智能忽略 IntelliSense文件,且确保Scripts文件夹中只有一种版本的JQuery。

运用捆绑包:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")

</head>
<body>
    @RenderBody()

    @Scripts.Render("~/bundles/clientfeaturesscripts")

@RenderSection("scripts", required: false)
</body> </html>

使用Scripts小节:

@section scripts
{
    <script src="~/Scripts/Home/MakeBooking.js" type="text/javascript"></script>
}

源代码:http://yunpan.cn/ccbVkY7qzC7ID 访问密码 077a

posted @ 2015-07-25 08:41  小居工作室  阅读(1007)  评论(0编辑  收藏  举报