ASP.NET MVC的动态CSS? [英] Dynamic CSS for ASP.NET MVC?

查看:394
本文介绍了ASP.NET MVC的动态CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像一般的.NET社区还没有对CSS的编译器拾取。在谷歌搜索我没有发现任何东西,甚至远程相关。

有没有人正在使用ASP.NET MVC想出了一个计划,以更智能地生成自己的CSS?我很想能够通过剃刀运行我的CSS例如,或为上海社会科学院获得过移植或者你有什么。也许我有一个新的侧项目我的手:)


解决方案

  

我很想能够通过剃刀运行我的CSS


什么阻止你?

 公共类CssViewResult:PartialViewResult
{
    公共覆盖无效的ExecuteReuslt(ControllerContext上下文)
    {
        context.HttpContext.Response.ContentType =文/ CSS
        base.ExecuteResult(上下文);
    }
}公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        返回新CssViewResult();
    }
}

〜/查看/主页/ Index.cshtml

  @ {
    VAR颜色=白;
    如果(DateTime.Now.Hour&GT 18 || DateTime.Now.Hour&下; 8)
    {
        颜色=黑色;
    }
}
包含.foo {
    颜色:@color;
}

现在所有剩下的就是将其包含:

 <链接HREF =@ Url.Action(指数)的rel =stylesheet属性类型=文/ CSS/>

您还可以使用强制类型来视图模型模板,写循环,如果,夹杂物,......

It looks like the .NET community in general has not picked up on CSS compilers. In searching Google I've not found anything even remotely relevant.

Has anyone that is using ASP.NET MVC figured out a scheme to more intelligently generate their CSS? I'd love to be able to run my CSS through Razor for example, or for SASS to get ported over or what have you. Maybe I have a new side project on my hands :)

解决方案

I'd love to be able to run my CSS through Razor

What stops you?

public class CssViewResult : PartialViewResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "text/css";
        base.ExecuteResult(context);
    }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return new CssViewResult();
    }
}

and in ~/Views/Home/Index.cshtml:

@{
    var color = "White";
    if (DateTime.Now.Hour > 18 || DateTime.Now.Hour < 8)
    {
        color = "Black";
    }
}
.foo {
    color: @color;
}

Now all that's left is to include it:

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

You can also make the template strongly typed to a view model, write loops, ifs, inclusions, ...

这篇关于ASP.NET MVC的动态CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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