ASP.NET MVC下Bundle的使用方法
ASP.NET MVC中Bundle是用于打包捆綁資源的(一般是css和js),它是在全局文件Global.asax.cs中注冊(cè)Bundle,而注冊(cè)的具體實(shí)現(xiàn)默認(rèn)是在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); 在應(yīng)用程序啟用時(shí)注冊(cè)Bundle
public class BundleConfig
{
// 有關(guān)綁定的詳細(xì)信息,請(qǐng)?jiān)L問 http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
// 使用要用于開發(fā)和學(xué)習(xí)的 Modernizr 的開發(fā)版本。然后,當(dāng)你做好
// 生產(chǎn)準(zhǔn)備時(shí),請(qǐng)使用 http://modernizr.com 上的生成工具來僅選擇所需的測(cè)試。
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
}
}
為了便于說明,這里在HomeController下新建一個(gè)Action,如下:
public ActionResult BundleTest()
{
return View();
}
這里以使用Bootstrap為例,在視圖中使用@Styles.Render() 和@Scripts.Render() 引入css和js,參數(shù)是在BundleConfig注冊(cè)的名稱
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>BundleTest</title>
@Styles.Render("~/Content/css")
</head>
<body>
@Scripts.Render("~/bundles/jquery", "~/bundles/bootstrap")
</body>
</html>
瀏覽頁面,查看源代碼,可以看到:

bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
由于在BundleConfig.cs中注冊(cè)上面的Bundle,@Styles.Render("~/Content/css")渲染時(shí)是引入~/Content/bootstrap.css和~/Content/site.css,js的渲染同理
為了驗(yàn)證是否真正引入了BootStrap的css與js資源,這里添加了一些簡(jiǎn)單的BootStrap示例代碼,如下:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>BundleTest</title>
@Styles.Render("~/Content/css")
</head>
<body>
<div class="container">
<div class="header clearfix">
<nav>
<ul class="nav nav-pills pull-right">
<li role="presentation" class="active"><a href="#">首頁</a></li>
<li role="presentation"><a href="#">關(guān)于我們</a></li>
<li role="presentation"><a href="#">聯(lián)系我們</a></li>
</ul>
</nav>
</div>
<form class="form-horizontal">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">用戶名</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" placeholder="用戶名">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">密碼</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="密碼">
</div>
</div>
<div class="form-group">
<label for="code" class="col-sm-2 control-label">驗(yàn)證碼</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="code" placeholder="驗(yàn)證碼">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> 記住我
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">登錄</button>
</div>
</div>
</form>
<footer class="footer">
<p>© 2017 Zhong.</p>
</footer>
</div> <!-- /container -->
@Scripts.Render("~/bundles/jquery", "~/bundles/bootstrap")
</body>
</html>
前臺(tái)瀏覽看效果(當(dāng)瀏覽器足夠大時(shí)是橫向平鋪的,如果將瀏覽器縮小,則是垂直平鋪,示例中的表單部分最能體現(xiàn)出來):


改進(jìn)
上面的Bundle是引入了未壓縮的css和js資源,但在實(shí)際應(yīng)用中,出于為了減輕服務(wù)器負(fù)載等原因,需要引入壓縮版的資源(一般是在未壓縮的命名后面加上min來命名,如jquery.js的壓縮版【有些叫法是精簡(jiǎn)版】是jquery.min.js)
于是修改BundleConfig.cs

重新編譯,再次瀏覽剛才的頁面,這時(shí)發(fā)現(xiàn)引入了壓縮版的資源(css/js)
注:由于示例時(shí)使用了ASP.NET MVC 5( .Net Framework 4.5),而在.net framework 4中的asp.net mvc 4可能會(huì)有下面的情況:
在頁面查看源代碼時(shí)發(fā)現(xiàn)腳本缺少引入~/Scripts/bootstrap.min.js,這是asp.net mvc 4使用的System.Web.Optimization.dll默認(rèn)使用了忽略規(guī)則*.min.js,這時(shí)可以在BundleConfig.cs的RegisterBundles中清除忽略規(guī)則

該解決方法一是通過反編譯System.Web.Optimization.dll并結(jié)合反編譯的代碼得出來的,另外也可以參考這個(gè)鏈接
另外就是在部署生產(chǎn)環(huán)境時(shí)發(fā)現(xiàn)無效,因?yàn)樯a(chǎn)環(huán)境不再是debug模式,此時(shí)需要設(shè)置:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net微信開發(fā)(高級(jí)群發(fā)文本)
這篇文章主要介紹了asp.net微信開發(fā)中有關(guān)高級(jí)群發(fā)文本的相關(guān)內(nèi)容,需要的朋友可以參考下2015-11-11
asp.net XML文件操作實(shí)現(xiàn)代碼
這幾天在項(xiàng)目中用到了XML文件配置存儲(chǔ)一些基本信息,如:參數(shù)、表格等一些信息存儲(chǔ)。由于記錄不是很多,所以用此文件來代替數(shù)據(jù)庫(kù)中設(shè)計(jì)的表結(jié)構(gòu)。2009-12-12
.Net實(shí)現(xiàn)上傳圖片按比例自動(dòng)縮小或放大的方法
這篇文章主要介紹了.Net實(shí)現(xiàn)上傳圖片按比例自動(dòng)縮小或放大的方法,實(shí)例內(nèi)容簡(jiǎn)潔功能實(shí)用,需要的朋友可以參考下2014-09-09
微信公眾平臺(tái)開發(fā)之地理位置.Net代碼解析
這篇文章主要為大家詳細(xì)解析了微信公眾平臺(tái)開發(fā)之地理位置.Net代碼,感興趣的小伙伴們可以參考一下2016-06-06
.NET Framework集成Quartz的實(shí)現(xiàn)示例
本文主要介紹了.NET Framework集成Quartz的實(shí)現(xiàn)示例,Quartz 主要用于定時(shí)執(zhí)行任務(wù)方面,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03
ASP.NET Session的七點(diǎn)認(rèn)識(shí)小結(jié)
ASP.NET Session的使用當(dāng)中我們會(huì)遇到很多的問題,那么這里我們來談下經(jīng)常出現(xiàn)的一些常用ASP.NET Session的理解2011-07-07

