我使用dotnetless(http://www.dotlesscss.org/)进行asp.netWeb表单应用程序,它运行良好.我喜欢使用颜色,字体大小等变量.但我可以看到变量值是静态的.
有没有办法使用dotnetless根据用户ID从数据库初始化这些变量值?
基本上我想将这个Web应用程序转换为基于主题的网站,因此每个用户都可以选择自己的颜色,字体,字体大小等.
任何方向将不胜感激.
这绝对是可能的,但不幸的是你不能从LESS本身查询你的数据库,因此你基本上需要为用户编写LESS文件并使用所需的变量值,然后加载它.
您可以在此处找到另一个答案的示例:https://stackoverflow.com/a/11628325/2330244
我认为,如果这是你想要的路径,你应该创建一个IHttpHandler来替换无点的LessCssHttpHandler.此处理程序将执行与LessCssHttpHandler基本相同的操作,但会在编译成css之前将数据库变量插入到较少的数据库中.
您可以查看Bundle Transformer LESS项目,该项目使用无点进行较少的翻译.它还有一个IHttpHandler,你可以将它作为你的基础.
正如其他人所说,这可能不是最好的行动方案
编辑: 处理程序的基本起点
public class LessHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var request = context.Request;
var response = context.Response;
var user = context.User;
string assetUrl = request.Url.LocalPath;
string assetPath = context.Server.MapPath(assetUrl);
//load less file into the data.
var data = "";
using (var file = new StreamReader(assetPath))
{
data = file.ReadToEnd();
}
DotlessConfiguration lessEngineConfig = DotlessConfiguration.GetDefault();
lessEngineConfig.MapPathsToWeb = false;
lessEngineConfig.CacheEnabled = false;
lessEngineConfig.DisableUrlRewriting = false;
lessEngineConfig.Web = false;
lessEngineConfig.MinifyOutput = true;
lessEngineConfig.LessSource = typeof(VirtualFileReader);
var lessEngineFactory = new EngineFactory(lessEngineConfig);
ILessEngine lessEngine = lessEngineFactory.GetEngine();
string vars = "";
//TODO set default for vars
if (user != null)
{
//TODO get vars for user
}
var content = lessEngine.TransformToCss(string.Format("{0}{1}", vars, data), null);
// Output text content of asset
response.ContentType = "text/css";
response.Write(content);
response.End();
}
public bool IsReusable
{
get { return true; }
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不创建一个自定义的.LESS插件,其中包含执行所需查找的功能?
下面的代码如图所示进行了测试.我实际上并没有在数据库中查找数据,但是所有必要的信息都应该可用.我验证了当在网站上以Windows身份验证模式运行时,我能够检索当前用户HttpContext.Current.User.Identity.Name.
要使用下面的函数,您可以在less文件中键入以下内容:
--lookup using custom function (hit db)
@brand_color:getCustomColor(usersThemeAttribute);
--then use the variable like normal
color: @brand_color;
Run Code Online (Sandbox Code Playgroud)
码
[DisplayName("UserProfilePlugin")]
public class UserProfilePlugin : IFunctionPlugin
{
public Dictionary<string, Type> GetFunctions()
{
return new Dictionary<string, Type>
{
{ "getCustomColor", typeof(GetCustomColorFunction) }
};
}
}
public class GetCustomColorFunction : Function
{
protected override Node Evaluate(Env env)
{
Guard.ExpectNumArguments(1, Arguments.Count(), this, Location);
Guard.ExpectNode<Keyword>(Arguments[0], this, Arguments[0].Location);
//the idea is that you would have many colors in a theme, this would be the name for a given color like 'bgColor', or 'foreColor'
var colorAttrName = Arguments[0] as Keyword;
//Lookup username
// string username = HttpContext.Current.User.Identity.Name;
//perform some kind of DB lookup using the username, get user theme info
// UserProfile up = new UserProfile();
// System.Drawing.Color c = up.GetColorByAttribute(colorAttrName.Value);
//return the appropriate color using RGB/etc...
// return new Color(c.R, c.G, c.B);
return new Color(129, 129, 129);
}
}
Run Code Online (Sandbox Code Playgroud)
要注册插件,请将其添加到web.config:
<dotless cache="false" >
<plugin name="UserProfilePlugin" assembly="Your.Assebly.Name" />
</dotless>
Run Code Online (Sandbox Code Playgroud)
考虑禁用无点的缓存,以便更改用户立即生效.
链接:https://github.com/dotless/dotless/wiki/FunctionPlugins