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.net core 自定视图主题 实现IViewLocationExpander接口的更多相关文章
- ASP.NET Core Razor 布局视图 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core Razor 布局视图 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Razor 布局视图 上一章节中我们学习了如何使用 EF ...
- 移花接木:借助 IViewLocationExpander 更换 ASP.NET Core View Component 视图路径
端午节在家将一个 asp.net 项目向 asp.net core 迁移时遇到了一个问题,用 view component 取代 Html.RenderAction 之后,运行时 view compo ...
- [译]ASP.NET Core 2.0 视图引擎
问题 如何在ASP.NET Core 2.0中使用Razor引擎来创建视图? 答案 新建一个空项目,修改Startup.cs,添加MVC服务和请求中间件: public void ConfigureS ...
- [译]ASP.NET Core 2.0 视图组件
问题 如何在ASP.NET Core 2.0中使用视图组件? 答案 新建一个空项目,修改Startup类并添加MVC服务和中间件: public void ConfigureServices(ISer ...
- ASP.NET Core 入门教程 7、ASP.NET Core MVC 分部视图入门
一.前言 1.本教程主要内容 ASP.NET Core MVC (Razor)分部视图简介 ASP.NET Core MVC (Razor)分部视图基础教程 ASP.NET Core MVC (Raz ...
- ASP.NET Core MVC 之视图(Views)
ASP.NET Core MVC 控制器可以使用视图返回格式化的结果. 1.什么是视图 在 MVC 中,视图封装了用户与应用交互呈现细节.视图是具有生成要发送到客户端内容的,包含嵌入代码的HTML模板 ...
- ASP.NET Core MVC 之视图组件(View Component)
1.视图组件介绍 视图组件是 ASP.NET Core MVC 的新特性,类似于局部视图,但它更强大.视图组件不使用模型绑定,并且仅依赖于调用它时所提供的数据. 视图组件特点: 呈块状,而不是整个响应 ...
- ASP.NET Core 入门笔记8,ASP.NET Core MVC 分部视图入门
一.前言 1.本教程主要内容 ASP.NET Core MVC (Razor)分部视图简介 ASP.NET Core MVC (Razor)分部视图基础教程 ASP.NET Core MVC (Raz ...
- #asp.net core mvc 的视图注入
View injection is the most useful feature introduced in ASP.NET Core. 1.添加一个FruitsService public cla ...
随机推荐
- 此上下文中不允许异步操作。启动异步操作的页必须将 Async 特性设置为 true,并且异步操作只能在 PreRenderComplete 事件之前的页上启动。
<%@ Page Language="C#" AutoEventWireup="true" ...... Async="true" % ...
- 第九章 Servlet工作原理解析(待续)
从 Servlet容器说起 创建 Servlet实例 Servlet体系结构 Servlet如何工作 Servlet中的Listener Filter如何工作 Servlet中的url-pattern
- DDD学习笔录——提炼问题域之知识提炼与协作
提炼问题域的意义 理解一个复杂问题域以便创建简单且有用的模型需要深入详尽的知识以及深刻的见解,这些只能通过与从内到外理解该领域的人协作得到.对模型的设计进行连续实验和探究正是DDD的能力所能实现的.只 ...
- leetcode479
public class Solution { public int LargestPalindrome(int n) { ) ; , n) - ; ; v > max / ; v--) { S ...
- Android Binder 系统学习笔记(一)Binder系统的基本使用方法
1.什么是RPC(远程过程调用) Binder系统的目的是实现远程过程调用(RPC),即进程A去调用进程B的某个函数,它是在进程间通信(IPC)的基础上实现的.RPC的一个应用场景如下: A进程想去打 ...
- Linux服务器在外地,如何用eclipse连接hdfs
配置外网和内网的映射,内部所有配置全部用内网的IP 本地所有配置皆为外网地址 本地给服务器发指令全部由映射转换为内网指定IP,即可
- C程序设计语言(K&R) 笔记1
当作复习... (1)将华氏度 换算成 摄氏度,公式: ℃=(5/9)(̧°F-32) #include <stdio.h> int transformTemprature(int F){ ...
- git pull没有指定branch的报错
执行git pull或者git push的时,有时候会出现如下报错: $ git pull You asked me to pull without telling me which branch y ...
- java格式化数字、货币、金钱
网上摘来的,以后可能会用到 java开发中经常会有数字.货币金钱等格式化需求,货币保留几位小数,货币前端需要加上货币符号等.可以用java.text.NumberFormat和java.text.De ...
- Spring集成MyBatis01 【推荐使用】、springMVC中文乱码和json转换问题
1 导包 1.1 spring-webmvc : spring框架包(当然里面也包含springmvc) 1.2 mybatis : mybatis框架包 1.3 mybatis-spring : s ...