• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.netcoreTheme中间件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

asp.net core中自定义视图引擎,继承接口 IViewLocationExpander

public class ThemeViewLocationExpander : IViewLocationExpander
    {
        public const string ThemeKey = "Theme";
        public void PopulateValues(ViewLocationExpanderContext context)
        {
            string theme = context.ActionContext.HttpContext.Items[ThemeKey].ToString();
            context.Values[ThemeKey] = theme;
        }
        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            string theme;

            if (context.Values.TryGetValue(ThemeKey, out theme))
            {
                viewLocations = new[] 
                {
                    $"/Themes/{theme}/Views/{{1}}/{{0}}.cshtml",
                    $"/Themes/{theme}/Views/Shared/{{0}}.cshtml",
                    $"/Themes/{theme}/Areas/{{2}}/Views/{{1}}/{{0}}.cshtml",
                    $"/Themes/{theme}/Areas/{{2}}/Views/Shared/{{0}}.cshtml",
                }
                .Concat(viewLocations);
            }
            return viewLocations;
        }
    }

新建中间件ThemeMiddleware

public class ThemeMiddleware
    {
        private readonly RequestDelegate _next;
        public IConfiguration _configuration;
        public ThemeMiddleware(RequestDelegate next, IConfiguration configuration)
        {
            _next = next;
            _configuration = configuration;
        }
        public Task Invoke(HttpContext context)
        {
            var folder = _configuration.GetSection("theme").Value;
            context.Request.HttpContext.Items[ThemeViewLocationExpander.ThemeKey] = folder ?? "Default";
            return _next(context);
        }
    }

中间件扩展

    public static class MiddlewareExtensions
    {
        /// <summary>
        /// 启用Theme中间件
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseTheme(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<ThemeMiddleware>();
        }
    }

中间件服务扩展

public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 添加Theme服务
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddTheme(this IServiceCollection services)
        {
            return services.Configure<RazorViewEngineOptions>(options => {
                options.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
            });
        }
    }

 

使用:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddTheme(); //添加Theme服务
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
          

            app.UseTheme();//启用theme中间件
            app.UseMvcWithDefaultRoute();
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【ASP.NET】解决HyperLink的NavigateUrl显示不正确的问题发布时间:2022-07-10
下一篇:
ASP.NET图形验证码的生成发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap