基於MVC+EasyUI的Web開發架構經驗總結(11)--使用Bundles處理簡化頁面代碼

來源:互聯網
上載者:User

標籤:web   開發   mvc   

在Web開發的時候,我們很多時候,需要引用很多CSS檔案、JS檔案,隨著使用更多的外掛程式或者獨立樣式檔案,可能我們的Web介面代碼會越來越臃腫,看起來也很累贅,在MVC裡面提供了一個Bundle的對象,用來簡化頁面代碼非常方便,本文主要介紹在我的MVC架構裡面,如何使用bundles來簡化頁面的代碼的。

1、常規的頁面代碼

我們知道,隨著使用更多的一些效果,我們可能不斷引入一些新的JS和CSS檔案,已達到Web介面更好的表現效果。這樣也就逐步增加了檔案代碼的行數,造成相對比較臃腫的情境,如下面的我正常使用的Web介面,頭部需要引入很多JS和CSS檔案。

@*添加Jquery EasyUI的樣式*@<link href="~/Content/JqueryEasyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" /><link href="~/Content/JqueryEasyUI/themes/icon.css" rel="stylesheet" type="text/css" /><link href="~/Content/themes/Default/style.css" rel="stylesheet" type="text/css" /><link href="~/Content/themes/Default/default.css" rel="stylesheet" type="text/css" />@*添加Jquery,EasyUI和easyUI的語言套件的JS檔案*@<script type="text/javascript" src="~/Content/JqueryEasyUI/jquery.min.js"></script><script type="text/javascript" src="~/Content/JqueryEasyUI/jquery.easyui.min.js"></script><script type="text/javascript" src="~/Content/JqueryEasyUI/locale/easyui-lang-zh_CN.js"></script>@*日期格式的引用*@<script src="~/Content/datapattern.js"></script><!--引用EasyUI擴充--><link href="~/Content/JQueryTools/jQuery.easyui-extend/extend/themes/easyui.extend.css" rel="stylesheet" /><link href="~/Content/JQueryTools/jQuery.easyui-extend/extend/themes/icon.css" rel="stylesheet" /><script src="~/Content/JQueryTools/jQuery.easyui-extend/jquery.easyui.extend.min.js"></script>@*引用提示控制項*@<link rel="stylesheet" type="text/css" href="~/Content/JQueryTools/jNotify/jquery/jNotify.jquery.css" media="screen" /><script type="text/javascript" src="~/Content/JQueryTools/jNotify/jquery/jNotify.jquery.js"></script>@*常用的一些組件業務指令碼函數*@<script type="text/javascript" src="~/Scripts/ComponentUtil.js"></script>

然後這樣的檔案總是在不斷的複製做,非常不雅觀,維護也不方便。

在ASP.NET MVC出來之後,引入了一個叫做Bundle的東西,它用來將js和css檔案捆綁為一個塊進行輸出,能夠極大簡化介面代碼,並預設對這些內容進行壓縮處理,提高效率。

最終簡化的介面代碼如下所示。

@using System.Web.Optimization;@Scripts.Render("~/bundles/jquery")@Styles.Render("~/Content/css")@Scripts.Render("~/bundles/jquerytools")@Styles.Render("~/Content/jquerytools")


2、使用bundles最佳化的介面操作

為了實現上面的效果,我們需要進行幾步的操作處理。

在App_Start裡面的BundleConfig裡面增加幾行處理代碼,如下所示。

650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/041059452203895.png" style="border:0px;" />

public class BundleConfig{// 有關 Bundling 的詳細資料,請訪問 http://go.microsoft.com/fwlink/?LinkId=254725public static void RegisterBundles(BundleCollection bundles){//為了減少太多的Bundles命名,定義的CSS的Bundle為:"~/Content/css"、"~/Content/jquerytools"//定義的Script的Bundles為:"~/bundles/jquery"、"~/bundles/jquerytools"//Jquery必備的StyleBundle和ScriptBundleStyleBundle css = new StyleBundle("~/Content/css");ScriptBundle jquery = new ScriptBundle("~/bundles/jquery");//添加Jquery EasyUI的樣式css.Include("~/Content/JqueryEasyUI/themes/default/easyui.css", "~/Content/JqueryEasyUI/themes/icon.css","~/Content/themes/Default/style.css","~/Content/themes/Default/default.css");//添加Jquery,EasyUI和easyUI的語言套件的JS檔案,日期格式的引用jquery.Include("~/Content/JqueryEasyUI/jquery.min.js","~/Content/JqueryEasyUI/jquery.easyui.min.js","~/Content/JqueryEasyUI/locale/easyui-lang-zh_CN.js","~/Content/datapattern.js");//常用的一些組件業務指令碼函數(建議放到最後)jquery.Include("~/Scripts/ComponentUtil.js");//擴充的StyleBundle和ScriptBundleStyleBundle cssExtend = new StyleBundle("~/Content/jquerytools");ScriptBundle jqueryExtend = new ScriptBundle("~/bundles/jquerytools");//引用EasyUI擴充cssExtend.Include("~/Content/JQueryTools/jQuery.easyui-extend/extend/themes/easyui.extend.css","~/Content/JQueryTools/jQuery.easyui-extend/extend/themes/icon.css");jqueryExtend.Include("~/Content/JQueryTools/jQuery.easyui-extend/jquery.easyui.extend.min.js");//引用訊息提示控制項cssExtend.Include("~/Content/JQueryTools/jNotify/jquery/jNotify.jquery.css");jqueryExtend.Include("~/Content/JQueryTools/jNotify/jquery/jNotify.jquery.js");//其他一些輔助指令碼和樣式//全部增加到集合裡面去bundles.Add(css);bundles.Add(jquery);bundles.Add(cssExtend);bundles.Add(jqueryExtend);}}

上面代碼,我們增加一些必要的Jquery和一些擴充給的JqueryTool的指令碼和樣式,方便統一化管理。

預設的情況下,Bundle是按照字母順序進行排序的,如果需要按照增加的次序進行排序,這需要寫一個自訂的定序進行處理,如下所示

/// <summary>/// 自訂Bundles排序/// </summary>internal class AsIsBundleOrderer : IBundleOrderer{public virtual IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files){return files;}}

然後在調用的時候,修改對象的定序即可。

ScriptBundle jqueryExtend = new ScriptBundle("~/bundles/jquerytools");jqueryExtend.Orderer = new AsIsBundleOrderer();

接著在Global.asa.cs裡面,增加對Bundle的註冊,如下所示。

protected void Application_Start(){AreaRegistration.RegisterAllAreas();BundleConfig.RegisterBundles(BundleTable.Bundles);WebApiConfig.Register(GlobalConfiguration.Configuration);FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);}

最後在MVC的視圖裡面,就可以使用Bundle來簡化介面代碼了。簡化後的介面代碼如下所示。

<!DOCTYPE html><html>    <head>    <title>使用者管理</title>    <meta name="viewport" content="width=device-width" />    @using System.Web.Optimization;    @Scripts.Render("~/bundles/jquery")    @Styles.Render("~/Content/css")    @Scripts.Render("~/bundles/jquerytools")    @Styles.Render("~/Content/jquerytools")...............

運行介面,雖然使用了簡化版本的代碼,依舊正常運行

650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/041206106119501.png" style="border:0px;" /> 

頁面代碼輸出則還是和原先未最佳化的一致。

<!DOCTYPE html><html>    <head>    <title>使用者管理</title>    <meta name="viewport" content="width=device-width" />    <script src="/Content/JqueryEasyUI/jquery.min.js"></script>    <script src="/Content/JqueryEasyUI/jquery.easyui.min.js"></script>    <script src="/Content/JqueryEasyUI/locale/easyui-lang-zh_CN.js"></script>    <script src="/Content/datapattern.js"></script>    <script src="/Scripts/ComponentUtil.js"></script>    <link href="/Content/JqueryEasyUI/themes/default/easyui.css" rel="stylesheet"/>    <link href="/Content/JqueryEasyUI/themes/icon.css" rel="stylesheet"/>    <link href="/Content/themes/Default/style.css" rel="stylesheet"/>    <link href="/Content/themes/Default/default.css" rel="stylesheet"/>    <script src="/Content/JQueryTools/jQuery.easyui-extend/jquery.easyui.extend.min.js"></script>    <script src="/Content/JQueryTools/jNotify/jquery/jNotify.jquery.js"></script>    <link href="/Content/JQueryTools/jQuery.easyui-extend/extend/themes/easyui.extend.css" rel="stylesheet"/>    <link href="/Content/JQueryTools/jQuery.easyui-extend/extend/themes/icon.css" rel="stylesheet"/>    <link href="/Content/JQueryTools/jNotify/jquery/jNotify.jquery.css" rel="stylesheet"/>

本文出自 “伍華聰的部落格” 部落格,請務必保留此出處http://wuhuacong.blog.51cto.com/1779896/1829035

基於MVC+EasyUI的Web開發架構經驗總結(11)--使用Bundles處理簡化頁面代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.