问题
I would like to let a user select the background colour for a website and save the selected colour in a database. When the person logs in the background correct colour will be displayed.
Based on the following website, I am able to set the colour within the CssHandler.ashx
file. But, what is the best way to get the information from the database?
Site master page,
<link href="../../Content/CSSHandler.ashx?file=Site.css" rel="stylesheet" type="text/css" />
Site.css,
header
{
background-color:#BG_COLOR#;
}
CssHandler.ashx,
public class CssHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/css";
// Get the file from the query stirng
string File = context.Request.QueryString["file"];
// Find the actual path
string Path = context.Server.MapPath(File);
// Limit to only css files
if (System.IO.Path.GetExtension(Path) != ".css")
context.Response.End();
// Make sure file exists
if (!System.IO.File.Exists(Path))
context.Response.End();
// Open the file, read the contents and replace the variables
using (System.IO.StreamReader css = new System.IO.StreamReader(Path))
{
string CSS = css.ReadToEnd();
CSS = CSS.Replace("#BG_COLOR#","Blue");
context.Response.Write(CSS);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
回答1:
Creating multiple CSS files and substituting the < link > directive is much better, performance wise.
Down side is you will need to maintain several CSS files that all do the same thing with different colors. So what I would do is create a common css file. Then for each possible configuration (background color, what have you), create its own CSS file. // assuming a reasonable number of combinations
That way, clients will cache, and the webserver will be serving static files.
回答2:
Well, to keep in line with your posted implementation, I would say you don't need to pass the CSS file name to the handler. Just pull the user's ID out of the session and query the database for their background color instead of reading from a file. Alternatively, if you want to allow anonymous users to choose the color, just store it in a cookie which the handler checks.
So, replace...
// Get the file from the query stirng
string File = context.Request.QueryString["file"];
with...
// Get user ID from session
int userId = Convert.ToInt32(Session["UserId"]));
// Now, pull background color from database
or...
// Get background color preference from cookie
HttpCookie cookie = Request.Cookies["Preferences"];
string bgColor = cookie["BackgroundColor"];
and go from there.
回答3:
I think a better route would be to have one CSS file with various classes and pass a class name through to your body tag:
.black {background:#000}
.blue {background:#00f}
And either find a way to script the body tag so it renders <body class="black>
or create a new WebControl
that renders as <body>
(and give it a rendering option that looks to the context to work out what it should be doing.
These ways you can keep all your CSS in one place and you don't have to edit real code to change the colour for one particular class, you just edit the CSS.
来源:https://stackoverflow.com/questions/868616/asp-net-mvc-dynamic-style-sheet