国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

解讀ASP.NET 5 & MVC6系列教程(16):自定義View視圖文件查找邏輯

2024-07-10 13:30:52
字體:
來源:轉載
供稿:網友

之前MVC5和之前的版本中,我們要想對View文件的路徑進行控制的話,則必須要對IViewEngine接口的FindPartialViewFindView方法進行重寫,所有的視圖引擎都繼承于該IViewEngine接口,比如默認的RazorViewEngine。但新版本MVC6中,對視圖文件的路徑方式卻不太一樣了,目前有兩種方式,一種是通過RazorViewEngine,另外一種是通過新特性IViewLocationExpander接口。

通過RazorViewEngine來控制View路徑

在新版的RazorViewEngine中,該類提供了兩個虛屬性(AreaViewLocationFormatsViewLocationFormats),可以用于重寫控制,而不必再對FindPartialViewFindView方法進行重寫,示例如下:

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"; // 可通過其它條件,設置皮膚的種類      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"; // 可通過其它條件,設置皮膚的種類      return base.ViewLocationFormats.Select(f => f.Replace("/Views/", "/Views/" + theme + "/"));    }  }}

然后,通過修改MVcOptions的實例屬性ViewEngines即可完成對視圖引擎的替換,代碼如下:

services.AddMvc().Configure<MvcOptions>(options =>{  options.ViewEngines.Clear();  options.ViewEngines.Add(typeof(ThemeViewEngine));});

這樣,系統(tǒng)在查找視圖文件的時候,就會按照新注冊的ThemeViewEngine的邏輯來執(zhí)行。

通過IViewLocationExpander來控制View路徑

在MVC6中,微軟還提供了另外一種新的方式來控制View文件的路徑,那就是IViewLocationExpander接口,通過實現(xiàn)該接口即可實現(xiàn)自定義邏輯,并且也可以使用相關的上下文對象。示例如下:

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中,實現(xiàn)了2個方法分別是PopulateValuesExpandViewLocations,PopulateValues方法可以讓我們想ViewLocationExpanderContext上下文中添加響應的鍵值對以便后續(xù)使用,通過,我們可以利用通過該上下文對象,來查找ActionContextHttpContext對象,以便利用這些對象做響應的判斷操作;而ExpandViewLocations方法,只會在沒有View緩存或在View緩存里找不到對應key的View文件時才會調用該方法,在該方法內,我們可以動態(tài)返回視圖的位置。

最后,我們在Startup.cs里通過修改RazorViewEngineOptions實例對象的ViewLocationExpanders屬性,來實現(xiàn)注冊目的,代碼如下:

services.Configure<RazorViewEngineOptions>(options =>{  options.ViewLocationExpanders.Add(typeof(ThemViewLocationExpander));});
 


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湟中县| 沿河| 莱西市| 铜川市| 宜良县| 曲沃县| 福建省| 比如县| 胶南市| 五台县| 雷山县| 乐山市| 金乡县| 云和县| 南投市| 栾川县| 大兴区| 沙洋县| 塘沽区| 扎囊县| 交口县| 宁陵县| 西吉县| 桐乡市| 宁津县| 永仁县| 广饶县| 通山县| 灵石县| 榆社县| 霍山县| 高青县| 太谷县| 中西区| 荔波县| 潜山县| 尼勒克县| 宁强县| 师宗县| 开远市| 浦江县|