首页 新闻 会员 周边 捐助

关于在asp.net.core 下自定义ViewEngine

0
悬赏园豆:5 [已解决问题] 解决于 2016-11-13 12:51

  刚接触.core  想自定义ViewEngine 加载不同的视图  但是没找到相应的地方修改  以前mvc4的时候 是继承 razorViewEngine就可以解决问题  现在试了一下 不行  请教一下知道的同学 谢谢

SpeakHero的主页 SpeakHero | 初学一级 | 园豆:31
提问于:2016-11-12 21:06
< >
分享
最佳答案
0

参考 Custom View Engine in ASP.NET Core and MVC 6 with Dynamic View Location

public class MyRazorViewEngine : RazorViewEngine
{
    public MyRazorViewEngine(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 + "/"));
        }
    }
}
services.AddMvc().Configure<MvcOptions>(options =>
{
    options.ViewEngines.Clear();
    options.ViewEngines.Add(typeof(MyRazorViewEngine));
});
收获园豆:5
dudu | 高人七级 |园豆:29568 | 2016-11-13 10:39

谢谢 站长达人  我去反编译过 razorViewEngine  却没发现 可以重写的方法

SpeakHero | 园豆:31 (初学一级) | 2016-11-13 12:50
其他回答(1)
0

在.net core 2.0 AreaViewLocationFormats和ViewLocationFormats 这两个方法都不是虚方法,怎么解决。

tianfeng.cc | 园豆:212 (菜鸟二级) | 2017-09-13 10:10

/// <summary>
/// 自定视图主题 实现IViewLocationExpander接口
/// </summary>
public class ThemeViewLocationExpander : IViewLocationExpander
{
public ThemeViewLocationExpander()
{
}
/// <summary>
/// 主题名称
/// /Views/+ViewLocationExpanders
/// </summary>
public string ThemeName { get; set; } = "Default";

    public void PopulateValues([FromServices]ViewLocationExpanderContext context)
    {
        HttpContext httpcontext = context.ActionContext.HttpContext;
        string theme = httpcontext.Request.GetTheme();
        if (theme.IsNotNullOrEmpty())
        {
            ThemeName = theme;
        }
        context.Values["theme"] = ThemeName;
    }

    /// <summary>
    /// 主题切换
    /// </summary>
    /// <param name="theme"></param>
    /// <returns></returns>
    public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,
                                                           IEnumerable<string> viewLocations)
    {
        string n = string.Empty;
        foreach (string item in viewLocations)
        {
            n = item.ToLower();
            if (!n.Contains("/shared"))
            {
                n = n.Replace("{1}", $"theme/{context.Values["theme"]}/{{1}}");
            }
            yield return n;
        }

    }
}

services.AddMvc.AddRazorOptions(option =>
{
option.ViewLocationExpanders.Clear();
option.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
})

支持(0) 反对(0) SpeakHero | 园豆:31 (初学一级) | 2018-09-12 11:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册