MVC引导主题 [英] MVC Bootstrap Themes
问题描述
我想包括在我的MVC应用程序5的bootswatch主题主题支持。
I want to include theme support for the bootswatch themes in my MVC 5 app.
我要保存用户的主题和加载,当他们登录。
I want the users theme to be saved and loaded when they log in.
我已经延长我的用户类,包括主题,可以成功设置并保存编辑用户页面上的一个主题。
I have extended my user class to include the theme and can successfully set and save a theme on the edit user page.
public class User : IdentityUser
{
public string BootstrapTheme {get;set;}
}
在BootstrapTheme财产,我会保存引导CSS链接href属性。例如:〜/内容/ bootstrap.flatly.min.css
In the BootstrapTheme property I'll save the href attribute for the bootstrap css link. eg "~/Content/bootstrap.flatly.min.css"
该计划是设置主题布局页面。
The plan is to set the theme in the layout page.
<链接HREF =〜/内容/ bootstrap.spacelab.min.css的rel =stylesheet属性/>
我怎样才能做到这一点没有在每个页面加载查询数据库?
How can I do this without querying the database on every page load?
能够做到像<链接HREF =@ User.BootstrapTheme的rel =stylesheet属性/方式>
将是理想
下面是关于如何保存它使用localStorage的 HTTP一个页面的链接:// wdtz。组织/ bootswatch-主题selector.html
Here is a link on how to save it for one page using localstorage http://wdtz.org/bootswatch-theme-selector.html
推荐答案
您应该存储主题名称/ URL作为对用户的索赔,还不如部分用户
类:
You should store theme name/url as a claim on the user, not as part of the User
class:
await userManager.AddClaimAsync(user.Id, new Claim("MyApp:ThemeUrl", "~/Content/bootstrap.flatly.min.css"));
当用户登录时,这种说法被添加到饼干,你可以通过扩展方法访问:
When user is logged in, this claim is added to the cookie and you can access it through extension method:
public static String GetThemeUrl(this ClaimsPrincipal principal)
{
var themeClaim = principal.Claims.FirstOrDefault(c => c.Type == "MyApp:ThemeUrl");
if (themeClaim != null)
{
return themeClaim.Value;
}
// return default theme url if no claim is set
return "path/to/default/theme";
}
和在你看来,你会访问主题网址是这样的:
and in your view you would access theme url like this:
<link href="@ClaimsPrincipal.Current.GetThemeUrl()" rel="stylesheet" />
这是主要要求就是在cookie可用,所以没有多余的DB命中必需的。
Claims on principal are available in the cookie, so no extra DB hits required.
你可以保持用户一个另外的 BootstrapTheme
因为你已经做了,但是当用户登录,添加这个主题作为一个自称的身份:
As an alternative you can persist user BootstrapTheme
as you already have done, but when user is logging in, add this theme as a claim to the identity:
public async Task SignInAsync(IAuthenticationManager authenticationManager, ApplicationUser applicationUser, bool isPersistent)
{
authenticationManager.SignOut(
DefaultAuthenticationTypes.ExternalCookie,
DefaultAuthenticationTypes.ApplicationCookie);
var identity = await this.CreateIdentityAsync(applicationUser, DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim("MyApp:ThemeUrl", applicationUser.BootstrapTheme));
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
,然后访问通过上述方法扩展视图的索赔。我最近的博客上讲述类似的情况 - 你可以一看有一个更清楚地了解索赔是如何工作的。
And then access the claim in the view via above-mentioned extension method. I've blogged recently about similar scenario - you can have a look there for a bit more insight how claims work.
这篇关于MVC引导主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!