解讀ASP.NET 5 & MVC6系列(16):自定義View視圖文件查找邏輯
之前MVC5和之前的版本中,我們要想對(duì)View文件的路徑進(jìn)行控制的話,則必須要對(duì)IViewEngine接口的FindPartialView或FindView方法進(jìn)行重寫(xiě),所有的視圖引擎都繼承于該IViewEngine接口,比如默認(rèn)的RazorViewEngine。但新版本MVC6中,對(duì)視圖文件的路徑方式卻不太一樣了,目前有兩種方式,一種是通過(guò)RazorViewEngine,另外一種是通過(guò)新特性IViewLocationExpander接口。
通過(guò)RazorViewEngine來(lái)控制View路徑
在新版的RazorViewEngine中,該類(lèi)提供了兩個(gè)虛屬性(AreaViewLocationFormats和ViewLocationFormats),可以用于重寫(xiě)控制,而不必再對(duì)FindPartialView或FindView方法進(jìn)行重寫(xiě),示例如下:
- public class ThemeViewEngine : RazorViewEngine
- {
- public ThemeViewEngine(IRazorPageFactory pageFactory,
- IRazorViewFactory viewFactory,
- IViewLocationExpanderProvider viewLocationExpanderProvider,
- IViewLocationCache viewLocationCache)
- : base(pageFactory,
- viewFactory,
- viewLocationExpanderProvider,
- viewLocationCache)
- {
- }
- public override IEnumerable<string> AreaViewLocationFormats
- {
- get
- {
- var value = new Random().Next(0, 1);
- var theme = value == 0 ? "Theme1" : "Theme2"; // 可通過(guò)其它條件,設(shè)置皮膚的種類(lèi)
- return base.AreaViewLocationFormats.Select(f => f.Replace("/Views/", "/Views/" + theme + "/"));
- }
- }
- public override IEnumerable<string> ViewLocationFormats
- {
- get
- {
var value = new Random().Next(0, 1);
var theme = value == 0 ? "Theme1" : "Theme2"; // 可通過(guò)其它條件,設(shè)置皮膚的種類(lèi)
return base.ViewLocationFormats.Select(f => f.Replace("/Views/", "/Views/" + theme + "/"));
- }
- }
- }
- 然后,通過(guò)修改MVcOptions的實(shí)例屬性ViewEngines即可完成對(duì)視圖引擎的替換,代碼如下:
- services.AddMvc().Configure<MvcOptions>(options =>
- {
- options.ViewEngines.Clear();
- options.ViewEngines.Add(typeof(ThemeViewEngine));
- });
這樣,系統(tǒng)在查找視圖文件的時(shí)候,就會(huì)按照新注冊(cè)的ThemeViewEngine的邏輯來(lái)執(zhí)行。
通過(guò)IViewLocationExpander來(lái)控制View路徑
在MVC6中,微軟還提供了另外一種新的方式來(lái)控制View文件的路徑,那就是IViewLocationExpander接口,通過(guò)實(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 IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,
- IEnumerable<string> 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ù)使用,通過(guò),我們可以利用通過(guò)該上下文對(duì)象,來(lái)查找ActionContext和HttpContext對(duì)象,以便利用這些對(duì)象做響應(yīng)的判斷操作;而ExpandViewLocations方法,只會(huì)在沒(méi)有View緩存或在View緩存里找不到對(duì)應(yīng)key的View文件時(shí)才會(huì)調(diào)用該方法,在該方法內(nèi),我們可以動(dòng)態(tài)返回視圖的位置。
***,我們?cè)赟tartup.cs里通過(guò)修改RazorViewEngineOptions實(shí)例對(duì)象的ViewLocationExpanders屬性,來(lái)實(shí)現(xiàn)注冊(cè)目的,代碼如下:
- services.Configure<RazorViewEngineOptions>(options =>
- {
- options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander));
- });