HTML generator for .NET?

╄→гoц情女王★ 提交于 2019-12-01 16:56:40
Igor Brejc

I have similar feelings about template systems (see ASP.MVC: Implementing a non-templated view engine?), and after experimenting a little, I took the following approach:

  • Implemented a fluent HTML generator C# class which does not write directly to a stream, instead it constructs the whole HTML page as an in-memory object hierarchy.
  • Higher-level (reusable) pieces of HTML code (like complex input controls etc.) are implemented as separate classes and are inserted as nodes into this hierarchy, and can expand themselves into plain HTML nodes automatically.
  • MVC Views are then POCO C# classes which construct the HTML hierarchy and write it out into the response stream.

Some of the benefits (from my perspective):

  • You still retain control over HTML
  • Reusability, inheritance, encapsulation...
  • The result is auto-formatted (indents, XHTML etc.)
  • And best of all... no template scripting using a separate DSL.

There is something related - SharpDOM - it is C#-based internal DSL. You can use it with ASP.NET MVC as well as outside of it. Right now it supports only generating of HTML, the next release is going to have CSS support too.

I am not familiar with Seaside , but you can use many different view engines with ASP.NET MVC, e.g., NHaml. Perhaps you can evaluate those and see if one would fit your needs.

You can also create your own view engine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!