我們先來(lái)簡(jiǎn)單看看我們熟悉的ASP.NET MVC中是如何管理我們項(xiàng)目中的這些靜態(tài)文件呢?
為海口等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及??诰W(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站制作、??诰W(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
其實(shí)當(dāng)我們新建一個(gè)MVC的項(xiàng)目時(shí),已經(jīng)生成了一個(gè)“模板”讓我們參考,
這個(gè)“模板”就是App_Start下面的 BundleConfig.cs
1 public class BundleConfig 2 { 3 // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 4 public static void RegisterBundles(BundleCollection bundles) 5 { 6 bundles.Add(new ScriptBundle("~/bundles/jquery").Include( 7 "~/Scripts/jquery-{version}.js")); 8 bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( 9 "~/Scripts/jquery.validate*"));10 // Use the development version of Modernizr to develop with and learn from. Then, when you're11 // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.12 bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(13 "~/Scripts/modernizr-*"));14 bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(15 "~/Scripts/bootstrap.js",16 "~/Scripts/respond.js"));17 bundles.Add(new StyleBundle("~/Content/css").Include(18 "~/Content/bootstrap.css",19 "~/Content/site.css"));20 }21 }
其中的ScriptBundle和StyleBundle分別是用于管理js和css的類,這兩個(gè)類都是繼承了Bundle這個(gè)類!
它位于System.Web.Optimization程序集,如果想要用這個(gè)功能,記得添加引用喔!
那我們要怎么使用這個(gè)呢?
現(xiàn)在假設(shè)在根目錄下面有css和js兩個(gè)文件夾,里面分別存放著Style1.css、Style2.css和js1.js、js2.js
下面就來(lái)看看怎么把它交于Bundle管理
1 bundles.Add(new ScriptBundle("~/bundles/js").Include(2 "~/js/js1.js",3 "~/js/js2.js"));4 bundles.Add(new StyleBundle("~/bundles/css").Include(5 "~/css/Style1.css",6 "~/css/Style2.css"));
其中的“~/bundles/js”和"~/bundles/css"是虛擬路徑!
然后就是在頁(yè)面中使用(就是用我們剛才的虛擬路徑)
1 @Styles.Render("~/bundles/css")2 @Scripts.Render("~/bundles/js")
是不是很方便呢!更多關(guān)于Bundle的內(nèi)容可以參考
http://www.asp.net/mvc/overview/performance/bundling-and-minification
因?yàn)樗皇俏覀兘裉斓闹饕獌?nèi)容,只是拿來(lái)與Nancy中的靜態(tài)文件處理形成對(duì)比,便于我們的理解。
下面就來(lái)看看Nancy中的靜態(tài)文件怎么處理。
為了演示的方便,這里僅使用css。
先看看具體的使用,然后再簡(jiǎn)單分析其內(nèi)部的實(shí)現(xiàn)。
在這個(gè)應(yīng)用程序中添加我們需要的引用,這里可以根據(jù)前面介紹的,
按自己喜歡的方式、方法來(lái)添加Nancy相關(guān)的引用
老規(guī)矩:Modules文件夾、HomeModule.cs
1 public class HomeModule : NancyModule 2 { 3 public HomeModule() 4 { 5 Get["/"] = _ => 6 { 7 return View["index"]; 8 }; 9 10 Get["/default"] = _ =>11 {12 return View["default"];13 };14 15 Get["/custom"] = _ =>16 {17 return View["custom"];18 };19 20 Get["/other"] = _ =>21 {22 return View["other"];23 };24 25 Get["/sub"] = _ =>26 {27 return View["sub"];28 };29 }30 }
content下面的sytle.css內(nèi)容如下
1 body {background-color:#00ffff;}2 p {font-size:xx-large; }
assets和other下面的style.css內(nèi)容如下
1 body {background-color:#00ffff;}2 p {font-size:xx-large;color:#ff0000;}
assets/sub下面 的style.css內(nèi)容如下
1 body {background-color:#808080;}2 p {font-size:xx-large;color:#ff0000;}
老規(guī)矩:Views文件夾、Home文件夾
添加 index.html、default.html、custom.html、other.html、sub.html 五個(gè)頁(yè)面
index.html
default.html
custom.html
other.html
sub.html
新建DemoBootstrapper.cs,使其繼承DefaultNancyBootstrapper并且override我們的ConfigureConventions
1 public class DemoBootstrapper : DefaultNancyBootstrapper2 {3 protected override void ConfigureConventions(NancyConventions nancyConventions)4 {5 base.ConfigureConventions(nancyConventions);6 nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("assets"));7 }8 }
1、default.html 用的樣式是在content下面的,能正常加載樣式!
2、custom.html用的樣式是在assets下面的,能正常加載樣式!
3、other.html用的樣式是在other下面的,不能正常加載樣式!!
4、sub.html用的樣式是在assets/sub下面的,能正常加載樣式!
很明顯,結(jié)果有點(diǎn)出乎我們的意料,我們?cè)贑onvetion的配置中,只配置了一項(xiàng)!
就是對(duì)assets文件夾進(jìn)行了處理。其他都沒(méi)有手動(dòng)配置!
但是在content下面的樣式是能夠正常顯示的?。《鴒ther下面的是不能正常顯示的??!assets的子文件夾sub的樣式也正常顯示?。?/p>
這個(gè)給人貌似不是很合理的感覺。
看看Network的內(nèi)容會(huì)發(fā)現(xiàn)other下面的樣式表不是不能正常加載那么簡(jiǎn)單,而是直接給個(gè)404?。。?/p>
那我們就深入的去看看這里面到底發(fā)生了什么事吧!
fork一份Nancy的源碼,clone到本地,來(lái)看看個(gè)所以然。(其實(shí)上面的例子我就是在源碼上面添加的一個(gè)Demo)
首先看看我們今天的主題Conventions下面的東西
其中從名字就可以看出跟我們今天的主題靜態(tài)文件,相關(guān)的就有7個(gè)??!
但這并不是我們的出發(fā)點(diǎn),我們的出發(fā)點(diǎn)是下面這個(gè)!
1 protected override void ConfigureConventions(NancyConventions nancyConventions)2 {3 base.ConfigureConventions(nancyConventions);4 nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("assets"));5 }
Convention的配置指引著我們要先去看看NancyConvetions這個(gè)類
在其構(gòu)造方法中調(diào)用了 BuildDefaultConventions 這個(gè)方法
1 /// <summary>2 /// Initializes a new instance of the <see cref="NancyConventions"/> class.3 /// </summary>4 public NancyConventions()5 {6 this.BuildDefaultConventions();7 }
這就很明顯的告訴我們,無(wú)論如何,它都會(huì)有默認(rèn)的Conventions!!而且看了里面的實(shí)現(xiàn)
會(huì)發(fā)現(xiàn),默認(rèn)的Convention還不僅僅是一個(gè)?。《前鄠€(gè)。這里我們僅探討關(guān)于靜態(tài)文件的。
1 private void BuildDefaultConventions() 2 { 3 var defaultConventions = 4 AppDomainAssemblyTypeScanner.TypesOf<IConvention>(ScanMode.OnlyNancy); 5 this.conventions = defaultConventions 6 .Union(AppDomainAssemblyTypeScanner.TypesOf<IConvention>(ScanMode.ExcludeNancy)) 7 .Select(t => (IConvention)Activator.CreateInstance(t)); 8 foreach (var convention in this.conventions) 9 {10 convention.Initialise(this);11 }12 }
現(xiàn)在我們就該去找關(guān)于靜態(tài)文件的默認(rèn)Convetion
發(fā)現(xiàn)剛才的7個(gè)相關(guān)中,有一個(gè)DefaultStaticContentsConventions
它實(shí)現(xiàn)了IConvention接口(Nancy中基本都是接口化編程,很Nice??!)。
其中的初始化方法中
1 public void Initialise(NancyConventions conventions)2 {3 conventions.StaticContentsConventions = new List<Func<NancyContext, string, Response>>4 {5 StaticContentConventionBuilder.AddDirectory("Content")6 };7 }
是不是跟我們自定義配置幾乎相差無(wú)幾??!我想看到AddDirectory的參數(shù)"Content",大家也應(yīng)該都知道了
為什么我們的content下面的樣式,沒(méi)有配置都能正常加載(我去,它默認(rèn)都是content,能不正常加載么。。)
里面的StaticContentConventionBuilder又是何方神圣呢?
這個(gè)是靜態(tài)基于目錄的幫助類
里面有兩個(gè)主要的方法 AddDirectory和AddFile ,都是返回Func<NancyContext, string, Response>類型的東東。
看名字都已經(jīng)知道大概實(shí)現(xiàn)了什么東西,一個(gè)基于某個(gè)目錄,一個(gè)基于某個(gè)單獨(dú)的文件。
這里需要注意一下這兩個(gè)方法的參數(shù)!
還有一些其他的東西是用于拼接目錄和處理Cache的。
把這幾個(gè)重要的類看了一下,是不是對(duì)這個(gè)靜態(tài)文件的默認(rèn)配置也清晰了不少呢?
然后對(duì)自定義Convetion配置的理解也是類似的,所以這里就不再累贅了。
從"引導(dǎo)程序"的ConfigureConventions中可以知道,無(wú)論我們自定義多少個(gè)Convetion,
都是要添加到StaticContentsConventions這個(gè)集合中的。
文章標(biāo)題:Nancy之靜態(tài)文件處理
轉(zhuǎn)載來(lái)于:http://vcdvsql.cn/article8/jhgcip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、企業(yè)建站、企業(yè)網(wǎng)站制作、軟件開發(fā)、面包屑導(dǎo)航、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)