有没有生成 CSS 的视图助手?

发布于 2024-10-25 04:02:31 字数 197 浏览 2 评论 0原文

我喜欢 HTML 助手的想法,它允许我不必为常见元素编写所有 HTML,同时仍然给我完全控制权,保留模型视图分离并且可以强类型化。

我想知道是否可以以相同的方式生成 CSS 样式表或样式定义 - 在视图标记中使用助手?理想情况下,我希望根据一些面向对象的规则和定义生成一些 CSS。

我找不到任何除了样式表压缩和合并之外还能提供任何功能的解决方案。

I like the idea of HTML helpers, which allows me not to write all the HTML for common elements, while still gives me full control, preserves model-view separation and can be strongly typed.

I'm wondering if it is possible to generate CSS stylesheets or style definitions the same way - using helpers in view markup? Ideally, I would like to have some of the CSS generated based on some object-oriented rules and definitions.

I can't find any solutions that will offer anything more than stylesheet compression and merging.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-11-01 04:02:31

一种简单的方法是使用自定义视图结果:

public class CssViewResult : PartialViewResult
{
    private readonly object _model;
    public CssViewResult(object model)
    {
        _model = model;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        this.ViewData.Model = _model;
        base.ExecuteResult(context);
        context.HttpContext.Response.ContentType = "text/css";
    }
}

然后使用简单的控制器操作:

public ActionResult MyCss()
{
    SomeModel model = ...
    return new CssViewResult(model);
}

并在相应的 MyCss.cshtml 视图中:

@model AppName.Models.SomeModel
.foo {
    background-color: @Model.SomeColor;
}

然后:

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

On easy way would be to use a custom view result:

public class CssViewResult : PartialViewResult
{
    private readonly object _model;
    public CssViewResult(object model)
    {
        _model = model;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        this.ViewData.Model = _model;
        base.ExecuteResult(context);
        context.HttpContext.Response.ContentType = "text/css";
    }
}

and then a simple controller action:

public ActionResult MyCss()
{
    SomeModel model = ...
    return new CssViewResult(model);
}

and in the corresponding MyCss.cshtml view:

@model AppName.Models.SomeModel
.foo {
    background-color: @Model.SomeColor;
}

and then:

<link href="@Url.Action("MyCss", "SomeController")" rel="stylesheet" type="text/css" />
最终幸福 2024-11-01 04:02:31

您所要求的并非不可能,但默认配置的方式是不可能的。原因是 CSS 文件不像视图那样被“解释”。他们只是路过而已。

您可以编写一些处理程序来为您生成 CSS,但它不会像 HTML 帮助程序那样。

理论上,您可以编写一个 Html 帮助程序来内联插入 CSS,但这实际上违背了 CSS 背后的目的,因为现在您需要在多个文件中包含相同的 CSS,从而使视图更大并使用更多带宽。

所以基本上,答案是……不,没有什么比 CSS 的 Html 助手更好的了。它很可能不存在的原因是,它带来的痛苦超过了它的价值。

What you're asking for isn't impossible, but the way things are configured by default it wouldn't be possible. The reason is that CSS files are not "interpreted" like views are. They are just passed through.

You could write some handlers that generate CSS for you, but it wouldn't be anything like an HTML helper.

You could, theoretically, write an Html helper that will insert CSS inline, but that's really defeating the purpose behind CSS, because now you need to include the same CSS in multiple files, making your views larger and using more bandwidth.

So basically, the answer is.. No, there is nothing out there like Html helpers for CSS. And the reason it most likely doesn't exist is that it's more of a pain in the rear than it's worth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文