How to load CSS style based on user preferences?

On 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 the database.

I have the following method (not yet database related):

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");
}

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

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

Assume that this CssStyle () method executes a query on a database, and for each page load it executes a query. What is the best way to prevent these unnecessary requests? Perhaps a session?

+5
source share
3 answers

, , , , CSS Session. Session, , - , , .

Session:

var cssFile = Session["UserStyle"];

.

+3

, .

, .

0

-, , ? , , , , , .

, , .

, , , , - .

, , CSS , - :

public FileResult CssStyle(variant which)
{
    // Hash the function, date and which stylesheet to ensure global,
    // valid, unique, and easily replicated filename
    filename = md5("get_css_style" + today() + which) + ".css"
    if (!file_exists(filename))
    {
        // DB operation here
        // create file here using filename
    }
    return File(filename, "text/css");
}
0

All Articles