新建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接口的更多相关文章

  1. ASP.NET Core Razor 布局视图 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Razor 布局视图 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Razor 布局视图 上一章节中我们学习了如何使用 EF ...

  2. 移花接木:借助 IViewLocationExpander 更换 ASP.NET Core View Component 视图路径

    端午节在家将一个 asp.net 项目向 asp.net core 迁移时遇到了一个问题,用 view component 取代 Html.RenderAction 之后,运行时 view compo ...

  3. [译]ASP.NET Core 2.0 视图引擎

    问题 如何在ASP.NET Core 2.0中使用Razor引擎来创建视图? 答案 新建一个空项目,修改Startup.cs,添加MVC服务和请求中间件: public void ConfigureS ...

  4. [译]ASP.NET Core 2.0 视图组件

    问题 如何在ASP.NET Core 2.0中使用视图组件? 答案 新建一个空项目,修改Startup类并添加MVC服务和中间件: public void ConfigureServices(ISer ...

  5. ASP.NET Core 入门教程 7、ASP.NET Core MVC 分部视图入门

    一.前言 1.本教程主要内容 ASP.NET Core MVC (Razor)分部视图简介 ASP.NET Core MVC (Razor)分部视图基础教程 ASP.NET Core MVC (Raz ...

  6. ASP.NET Core MVC 之视图(Views)

    ASP.NET Core MVC 控制器可以使用视图返回格式化的结果. 1.什么是视图 在 MVC 中,视图封装了用户与应用交互呈现细节.视图是具有生成要发送到客户端内容的,包含嵌入代码的HTML模板 ...

  7. ASP.NET Core MVC 之视图组件(View Component)

    1.视图组件介绍 视图组件是 ASP.NET Core MVC 的新特性,类似于局部视图,但它更强大.视图组件不使用模型绑定,并且仅依赖于调用它时所提供的数据. 视图组件特点: 呈块状,而不是整个响应 ...

  8. ASP.NET Core 入门笔记8,ASP.NET Core MVC 分部视图入门

    一.前言 1.本教程主要内容 ASP.NET Core MVC (Razor)分部视图简介 ASP.NET Core MVC (Razor)分部视图基础教程 ASP.NET Core MVC (Raz ...

  9. #asp.net core mvc 的视图注入

    View injection is the most useful feature introduced in ASP.NET Core. 1.添加一个FruitsService public cla ...

随机推荐

  1. gridcontrol 添加行号

    private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndic ...

  2. C#连接MSSQL

    本文将介绍如何用C#连接MSSQL,C#连接SQL十分简单.我们一步一步来操作. 1.打开Microsoft SQL Server Management Studio创建一个数据库,这里我创建一个数据 ...

  3. volatile关键字在多线程中的作用

  4. eclipse java 注释模板配置详解

    设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...

  5. oracle行转列练习

    ----------------------第一题--------------------------- create table STUDENT_SCORE ( name ), subject ), ...

  6. win10获取超级管理员权限脚本实现

    建立一个TXT文件,把下面的脚本贴到里面,然后把后缀改成reg格式,双击添加到注册表就可以了, win10_1703版本亲测可用.... Windows Registry Editor Version ...

  7. 基于R语言的RRT算法效率统计

  8. pecl的使用

    1. 安装方法 : pecl install packagename, 安装目录在/usr/local/php7/lib/php/extensions 例如:pecl install sealog 2 ...

  9. jquery 仿文本编辑器(智能提示输入文字)

    1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InputAu ...

  10. python中list的使用

    1.list(列表)是一种有序的集合,可以随时添加.修改.删除其中的元素. 举例:listClassName = ['Jack','Tom','Mark'] 列表可以根据索引获取元素,如:listCl ...