Asp.net Core Theme 中间件

本文介绍了如何在ASP.NET Core中创建自定义视图引擎,通过继承IViewLocationExpander接口,并实现`ThemeViewLocationExpander`,结合配置中间件`ThemeMiddleware`和扩展方法,实现实时主题切换。同时展示了如何在`ConfigureServices`和`Configure`方法中集成这些组件。

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();
}
03-08
### MathPix工具介绍 Mathpix Snipping Tool 是一款强大的数学公式识别工具,从最初的原型发展至今已经历了多次迭代和功能增强[^1]。该工具不仅能处理简单的数学表达式,还可以应对复杂的数学模型以及多行公式,并且支持手写公式的识别。 这款应用程序通过不断的科技创新与用户反馈改进,在学术研究和技术领域赢得了良好声誉,成为许多科研人员不可或缺的工作伙伴之一。除了基本的功能外,Mathpix 还提供了多种高级特性来满足不同用户的特定需求。 ### 使用方法 #### 安装与启动 为了使用 Mathpix Snipping Tool ,首先需要下载安装程序并按照提示完成设置过程。一旦成功安装后即可随时调用此应用来进行截图操作。 #### 截取屏幕上的公式图像 当遇到想要转换成 LaTeX 或 AsciiMath 的图片时,只需打开软件界面按下快捷键(默认为 `Ctrl+Alt+M`),此时鼠标指针会变为十字形状以便于选取目标区域;框选所需部分之后松开按键即刻上传至云端服务器等待进一步分析处理。 #### 获取LaTeX代码片段 经过短暂几秒钟的时间延迟过后,所截获的内容将以纯文本形式显示出来供复制粘贴到其他编辑器当中继续编写文档或是分享给他人查看交流之用。 ```python import pyperclip # 假设已获取到LaTeX字符串 stored_in_variable named latex_code pyperclip.copy(latex_code) print("LaTeX code has been copied to clipboard.") ``` 对于希望深入探索更多可能性的用户来说,可以考虑查阅官方提供的扩展插件——如用于解析Markdown文件内的公式标记语法的库 **mathpix-markdown-it** [^2] ——这将进一步提升工作效率和个人体验感。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值