using AcdiuTools.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; namespace AcdiuTools.Controllers { /// /// 基础控制器 /// 所有需要主题功能的控制器应继承此类 /// public class BaseController(IThemeService themeService) : Controller { private readonly IThemeService _themeService = themeService; /// /// 在执行 Action 之前加载主题配置并注入 ViewBag /// public override async void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); // 异步操作在 OnActionExecuting 中处理需小心,通常建议在 Action 内部或中间件中处理 // 这里为了演示简单,使用同步等待或改为中间件方案更佳 // 更好的方式:使用 ViewComponent 或 TagHelper var theme = await _themeService.GetUserThemeAsync(); ViewBag.CurrentTheme = theme; } } }