码迷,mamicode.com
首页 > Web开发 > 详细

asp.net core 自定视图主题 实现IViewLocationExpander接口

时间:2018-09-12 13:12:14      阅读:550      评论:0      收藏:0      [点我收藏+]

标签:pop   rtu   实现   新建   default   virtual   expander   theme   enum   

新建ThemeViewLocationExpander.cs 实现IViewLocationExpander接口

 /// <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;
            }

        }
    }

 添加新的ViewLocationExpanders

 public class Startup{
  public virtual void ConfigureServices(IServiceCollection services)
        {
services.AddMvc().AddRazorOptions(option =>
    {
        option.ViewLocationExpanders.Clear();
        option.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
    })
    }
}

  

 

asp.net core 自定视图主题 实现IViewLocationExpander接口

标签:pop   rtu   实现   新建   default   virtual   expander   theme   enum   

原文地址:https://www.cnblogs.com/SpeakHero/p/9634172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!