之前MVC5和之前的版本中,我們要想對(duì)View文件的路徑進(jìn)行控制的話,則必須要對(duì)IViewEngine
接口的FindPartialView
或FindView
方法進(jìn)行重寫,所有的視圖引擎都繼承于該IViewEngine
接口,比如默認(rèn)的RazorViewEngine
。但新版本MVC6中,對(duì)視圖文件的路徑方式卻不太一樣了,目前有兩種方式,一種是通過RazorViewEngine
,另外一種是通過新特性IViewLocationExpander
接口。
通過RazorViewEngine來控制View路徑
在新版的RazorViewEngine
中,該類提供了兩個(gè)虛屬性(AreaViewLocationFormats
和ViewLocationFormats
),可以用于重寫控制,而不必再對(duì)FindPartialView
或FindView
方法進(jìn)行重寫,示例如下:
public class ThemeViewEngine : RazorViewEngine { public ThemeViewEngine(IRazorPageFactory pageFactory, IRazorViewFactory viewFactory, IViewLocationExpanderProvider viewLocationExpanderProvider, IViewLocationCache viewLocationCache) : base(pageFactory, viewFactory, viewLocationExpanderProvider, viewLocationCache) { } public override IEnumerablestring> AreaViewLocationFormats { get { var value = new Random().Next(0, 1); var theme = value == 0 ? "Theme1" : "Theme2"; // 可通過其它條件,設(shè)置皮膚的種類 return base.AreaViewLocationFormats.Select(f => f.Replace("/Views/", "/Views/" + theme + "/")); } } public override IEnumerablestring> ViewLocationFormats { get { var value = new Random().Next(0, 1); var theme = value == 0 ? "Theme1" : "Theme2"; // 可通過其它條件,設(shè)置皮膚的種類 return base.ViewLocationFormats.Select(f => f.Replace("/Views/", "/Views/" + theme + "/")); } } }
然后,通過修改MVcOptions的實(shí)例屬性ViewEngines即可完成對(duì)視圖引擎的替換,代碼如下:
services.AddMvc().ConfigureMvcOptions>(options => { options.ViewEngines.Clear(); options.ViewEngines.Add(typeof(ThemeViewEngine)); });
這樣,系統(tǒng)在查找視圖文件的時(shí)候,就會(huì)按照新注冊(cè)的ThemeViewEngine
的邏輯來執(zhí)行。
通過IViewLocationExpander來控制View路徑
在MVC6中,微軟還提供了另外一種新的方式來控制View文件的路徑,那就是IViewLocationExpander
接口,通過實(shí)現(xiàn)該接口即可實(shí)現(xiàn)自定義邏輯,并且也可以使用相關(guān)的上下文對(duì)象。示例如下:
public class ThemeViewLocationExpander : IViewLocationExpander { public void PopulateValues(ViewLocationExpanderContext context) { var value = new Random().Next(0, 1); var theme = value == 0 ? "Theme1" : "Theme2"; context.Values["theme"] = theme; } public virtual IEnumerablestring> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerablestring> viewLocations) { return viewLocations.Select(f => f.Replace("/Views/", "/Views/" + context.Values["theme"] + "/")); } }
在上述自定義的IViewLocationExpander
中,實(shí)現(xiàn)了2個(gè)方法分別是PopulateValues
和ExpandViewLocations
,PopulateValues
方法可以讓我們想ViewLocationExpanderContext
上下文中添加響應(yīng)的鍵值對(duì)以便后續(xù)使用,通過,我們可以利用通過該上下文對(duì)象,來查找ActionContext
和HttpContext
對(duì)象,以便利用這些對(duì)象做響應(yīng)的判斷操作;而ExpandViewLocations
方法,只會(huì)在沒有View緩存或在View緩存里找不到對(duì)應(yīng)key的View文件時(shí)才會(huì)調(diào)用該方法,在該方法內(nèi),我們可以動(dòng)態(tài)返回視圖的位置。
最后,我們?cè)?code>Startup.cs里通過修改RazorViewEngineOptions
實(shí)例對(duì)象的ViewLocationExpanders
屬性,來實(shí)現(xiàn)注冊(cè)目的,代碼如下:
services.ConfigureRazorViewEngineOptions>(options => { options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander)); });
標(biāo)簽:甘肅 海西 臨夏 聊城 慶陽 中衛(wèi) 清遠(yuǎn) 巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解讀ASP.NET 5 & MVC6系列教程(16):自定義View視圖文件查找邏輯》,本文關(guān)鍵詞 解讀,ASP.NET,amp,MVC6,系列,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。