分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

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

发布时间:2023-09-06 02:14责任编辑:苏小强关键词:暂无标签

新建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接口

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved