Files
AcdiuTools/Controllers/BaseController.cs
2026-04-25 00:29:16 +08:00

30 lines
1.0 KiB
C#

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