我应该如何加载基于用户设置一个CSS样式? [英] How should I load a CSS style based on user setting?

查看:196
本文介绍了我应该如何加载基于用户设置一个CSS样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC网站的人可以不同的CSS样式之间进行选择。在未来的这些CSS样式名称将被存储在数据库中。

In my ASP.NET MVC website people can choose between different css styles. In the future the name of these css styles will be stored in a database.

我有以下方法的(还不与数据库进行通信)

public FileResult CssStyle()
{
    string style = "/Content/wide-site.css";

    if (!String.IsNullOrWhiteSpace(style))
    {
        return File(style, "text/css");
    }

    return File("/Content/site.css", "text/css");
}



在我_Layout.cshtml我用这条线来加载样式表:

And in my _Layout.cshtml I use this line to load the stylesheet:

<link href="@Url.Action("CssStyle", "Home")" rel="stylesheet" type="text/css" />



假设这CssStyle()方法执行一个请求到数据库,并为每个页面加载它执行查询。什么是防止这些不必要的请求的最佳方式?会话吧?

Suppose that this CssStyle() method does a request to the database and for every page load it executes a query. What's the best way to prevent these unnecessary requests? A session perhaps?

推荐答案

运行查询时,它很重要,因此,例如在登录,然后保存CSS文件名会话。您还需要刷新会话变量,如果你有一个个人资料页面,在这里用户可以改变自己的风格 - 当他们改变这种方式,他们会立即看到新的风格。

Run the query when it matters, so for example on login, and then store the CSS file name in Session. You would also need to refresh the Session variable if you have a profile page where the users can change their style - that way when they change it they would see the new style immediately.

然后在你的方法,拉动该值从会话的:

Then in your method, pull that value out of Session:

var cssFile = Session["UserStyle"];

例如

这篇关于我应该如何加载基于用户设置一个CSS样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆