diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeManager.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeManager.cs index 2eb3130823..968c7aa5f5 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeManager.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeManager.cs @@ -1,52 +1,41 @@ using System; -using System.Linq; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.UI.Theming { - public class DefaultThemeManager : IThemeManager, ITransientDependency, IServiceProviderAccessor + public class DefaultThemeManager : IThemeManager, IScopedDependency, IServiceProviderAccessor { - public IServiceProvider ServiceProvider { get; } + private const string CurrentThemeHttpContextKey = "__AbpCurrentTheme"; + public IServiceProvider ServiceProvider { get; } public ITheme CurrentTheme => GetCurrentTheme(); - protected ThemingOptions Options { get; } + protected IThemeSelector ThemeSelector { get; } + protected IHttpContextAccessor HttpContextAccessor { get; } public DefaultThemeManager( - IOptions options, - IServiceProvider serviceProvider) + IServiceProvider serviceProvider, + IThemeSelector themeSelector, + IHttpContextAccessor httpContextAccessor) { + HttpContextAccessor = httpContextAccessor; ServiceProvider = serviceProvider; - Options = options.Value; + ThemeSelector = themeSelector; } protected virtual ITheme GetCurrentTheme() { - var themeInfo = GetCurrentThemeInfo(); - return (ITheme) ServiceProvider.GetRequiredService(themeInfo.ThemeType); - } - - protected virtual ThemeInfo GetCurrentThemeInfo() - { - if (!Options.Themes.Any()) - { - throw new AbpException($"No theme registered! Use {nameof(ThemingOptions)} to register themes."); - } - - if (Options.DefaultThemeName == null) - { - return Options.Themes.Values.First(); - } + var preSelectedTheme = HttpContextAccessor.HttpContext.Items[CurrentThemeHttpContextKey] as ITheme; - var themeInfo = Options.Themes.Values.FirstOrDefault(t => t.Name == Options.DefaultThemeName); - if (themeInfo == null) + if (preSelectedTheme == null) { - throw new AbpException("Default theme is configured but it's not found in the registered themes: " + Options.DefaultThemeName); + preSelectedTheme = (ITheme)ServiceProvider.GetRequiredService(ThemeSelector.GetCurrentThemeInfo().ThemeType); + HttpContextAccessor.HttpContext.Items[CurrentThemeHttpContextKey] = preSelectedTheme; } - return themeInfo; + return preSelectedTheme; } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeSelector.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeSelector.cs new file mode 100644 index 0000000000..e5f4329b47 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/DefaultThemeSelector.cs @@ -0,0 +1,37 @@ +using System.Linq; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Theming +{ + public class DefaultThemeSelector : IThemeSelector, ITransientDependency + { + protected ThemingOptions Options { get; } + + public DefaultThemeSelector(IOptions options) + { + Options = options.Value; + } + + public virtual ThemeInfo GetCurrentThemeInfo() + { + if (!Options.Themes.Any()) + { + throw new AbpException($"No theme registered! Use {nameof(ThemingOptions)} to register themes."); + } + + if (Options.DefaultThemeName == null) + { + return Options.Themes.Values.First(); + } + + var themeInfo = Options.Themes.Values.FirstOrDefault(t => t.Name == Options.DefaultThemeName); + if (themeInfo == null) + { + throw new AbpException("Default theme is configured but it's not found in the registered themes: " + Options.DefaultThemeName); + } + + return themeInfo; + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/IThemeSelector.cs b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/IThemeSelector.cs new file mode 100644 index 0000000000..d2f3670493 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI/UI/Theming/IThemeSelector.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.AspNetCore.Mvc.UI.Theming +{ + public interface IThemeSelector + { + ThemeInfo GetCurrentThemeInfo(); + } +} \ No newline at end of file