zoukankan      html  css  js  c++  java
  • asp.net core 自定视图主题 实现IViewLocationExpander接口

    新建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中使用事务控制
    C语言学习笔记——两个数交换位置的多种方式
    PHP字符串函数
    PHP笔记——文件处理
    算法——穿越沙漠算法
    C学习笔记——使用CL编译器
    Wordpress——一些内部参数记录
    C语言笔记——原码、反码、补码
    C学习笔记——内存
    Linux VI编辑器命令集合
  • 原文地址:https://www.cnblogs.com/SpeakHero/p/9634172.html
Copyright © 2011-2022 走看看