• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    asp.net Bundle功能扩展
    前言
    新建Asp.net MVC4项目的时候,在Global.asax.cs里面发现多了一句代码
    BundleConfig.RegisterBundles(BundleTable.Bundles)
    google了以后终于弄清楚了这个的作用,发现这个东西确实非常实用,且功能强大,能够压缩合并js和CSS,但是目前的使用起来不是特别好,如果添加js或者css文件的话,需要修改BundleConfig的代码。
    这里我自己简单修改了BundleConfig,对这个进行简单的扩展。
    下面贴出代码
    先贴配置文件BundleConfig.xml(文件放在网站目录下路径见代码中变量BundleConfigPath)
    复制代码 代码如下:

    ?xml version="1.0" encoding="utf-8" ?>
    root>
    Scripts>
    Script Path="~/bundles/jquery">
    File>~/Scripts/jquery-{version}.js/File>
    /Script>
    Script Path="~/bundles/jqueryui">
    File>~/Scripts/jquery-ui-{version}.js/File>
    /Script>
    Script Path="~/bundles/jqueryval">
    File>~/Scripts/jquery.unobtrusive*/File>
    File>~/Scripts/jquery.validate*/File>
    /Script>
    Script Path="~/bundles/modernizr">
    File>~/Scripts/modernizr-*/File>
    /Script>
    Script Path="~/bb/aa">
    File>~/Views/Home/addda.js/File>
    /Script>
    /Scripts>
    Styles>
    Style Path="~/Content/themes/base/css">
    File>~/Content/themes/base/jquery.ui.core.css/File>
    File>~/Content/themes/base/jquery.ui.resizable.css/File>
    File>~/Content/themes/base/jquery.ui.selectable.css/File>
    File>~/Content/themes/base/jquery.ui.accordion.css/File>
    File>~/Content/themes/base/jquery.ui.autocomplete.css/File>
    File>~/Content/themes/base/jquery.ui.button.css/File>
    File>~/Content/themes/base/jquery.ui.dialog.css/File>
    File>~/Content/themes/base/jquery.ui.slider.css/File>
    File>~/Content/themes/base/jquery.ui.tabs.css/File>
    File>~/Content/themes/base/jquery.ui.datepicker.css/File>
    File>~/Content/themes/base/jquery.ui.progressbar.css/File>
    File>~/Content/themes/base/jquery.ui.theme.css/File>
    /Style>
    Style Path="~/Content/css">
    File>~/Content/site.css/File>
    /Style>
    /Styles>
    /root>

    代码文件:BundleConfig.cs
    复制代码 代码如下:

    public class BundleConfig
    {
    public static string BundleConfigPath = "~/Config/BundleConfig.xml";
    /// summary>
    /// Register Bundles From XML
    /// /summary>
    /// param name="bundles">/param>
    public static void RegisterBundles(BundleCollection bundles)
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(HttpContext.Current.Server.MapPath(BundleConfigPath));
    XmlNode root = doc.DocumentElement;
    // Regester Script
    XmlNodeList ScriptList = root.SelectNodes("Scripts/Script");
    if (ScriptList != null ScriptList.Count > 0)
    {
    foreach (XmlNode node in ScriptList)
    {
    string path = CheckNodeRegedit(node);
    if (string.IsNullOrEmpty(path)) continue;
    var bound = new ScriptBundle(path);
    Liststring> files = GetFilesFormNode(node);
    if (files.Count > 0)
    {
    bound.Include(files.ToArray());
    bundles.Add(bound);
    }
    }
    }
    // Regester Style
    XmlNodeList StyleList = root.SelectNodes("Styles/Style");
    if (StyleList != null StyleList.Count > 0)
    {
    foreach (XmlNode node in StyleList)
    {
    string path = CheckNodeRegedit(node);
    if (string.IsNullOrEmpty(path)) continue;
    var bound = new StyleBundle(path);
    Liststring> files = GetFilesFormNode(node);
    if (files.Count > 0)
    {
    bound.Include(files.ToArray());
    bundles.Add(bound);
    }
    }
    }
    doc = null;
    }
    /// summary>
    /// 如果内容为空则不添加
    /// /summary>
    /// param name="node">/param>
    /// returns>/returns>
    private static Liststring> GetFilesFormNode(XmlNode node)
    {
    Liststring> files = new Liststring>();
    foreach (XmlNode nodeFile in node.ChildNodes)
    {
    if (!string.IsNullOrEmpty(nodeFile.InnerText.Trim()))
    files.Add(nodeFile.InnerText.Trim());
    }
    return files;
    }
    /// summary>
    /// 检查注册的Node
    /// /summary>
    /// param name="node">/param>
    /// returns>/returns>
    private static string CheckNodeRegedit(XmlNode node)
    {
    XmlAttribute pathAtt = node.Attributes["Path"];
    string path = string.Empty;
    if (pathAtt == null || string.IsNullOrEmpty(pathAtt.Value.Trim()) || node.ChildNodes.Count == 0)
    return string.Empty;
    else
    return pathAtt.Value.Trim();
    }
    }
    您可能感兴趣的文章:
    • 使用asp.net MVC4中的Bundle遇到的问题及解决办法分享
    • 基于Asp.Net MVC4 Bundle捆绑压缩技术的介绍
    • ASP.NET MVC Bundles 用法和说明(打包javascript和css)
    • ASP.NET MVC中使用Bundle打包压缩js和css的方法
    • Asp.net程序优化js、css实现合并与压缩的方法
    • 浅谈ASP.NET中MVC 4 的JS/CSS打包压缩功能
    • Asp.net MVC下使用Bundle合并、压缩js与css文件详解
    上一篇:CreateOutputCachedItemKey 缓存key的创建
    下一篇:C# 定义常量 两种实现方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    asp.net Bundle功能扩展 asp.net,Bundle,功能,扩展,asp.net,