有没有生成 CSS 的视图助手?
我喜欢 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种简单的方法是使用自定义视图结果:
然后使用简单的控制器操作:
并在相应的
MyCss.cshtml
视图中:然后:
On easy way would be to use a custom view result:
and then a simple controller action:
and in the corresponding
MyCss.cshtml
view:and then:
您所要求的并非不可能,但默认配置的方式是不可能的。原因是 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.