为什么使用 TagBuilder 而不是 StringBuilder?
使用标签生成器和字符串生成器在 htmlhelper 类中创建表或使用 HtmlTable 有什么区别?
他们不是产生同样的东西吗?
what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable?
aren't they generating the same thing??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
TagBuilder
是专门为创建 html 标签及其内容而设计的类。你说得对,结果无论如何都是一个字符串,当然你仍然可以使用StringBuilder
并且结果将是相同的,但是你可以使用TagBuilder
做得更容易。假设您需要生成一个标签:使用
StringBuilder
您需要编写如下内容:这不是很酷,不是吗?
并比较如何使用
TagBuilder
构建它;那不是更好吗?
TagBuilder
is a class that specially designed for creating html tags and their content. You are right saying that result will be anyway a string and of course you still can useStringBuilder
and the result will be the same, but you can do things easier withTagBuilder
. Lets say you need to generate a tag:Using
StringBuilder
you need to write something like this:It is not very cool, isn’t it?
And compare how you can build it using
TagBuilder
;Isn't that better?
这只是方便而已。来自本教程 :
查看 TagBuilder 上的方法,并考虑它们是否给您带来价值。您是否希望每次都在
StringBuilder
中手动执行相同的操作?它对你来说有逃避的意义吗?属性合并等等?生成的代码是否更易于阅读,是否更清楚地表明您正在构建标签而不是某个任意字符串?It's just convenience. From this tutorial:
Look at the methods on TagBuilder, and think about whether they give you value. would you want to do the same thing yourself manually in
StringBuilder
every time? Is there escaping that it does for you? Attribute merging, etc? Is the resulting code easier to read, making it clearer that you're building a tag rather than some arbitrary string?到目前为止,其他答案都忽略了一点。如果您从扩展方法返回
TagBuilder
,您可以继续在视图中添加属性。假设您从 Html 助手返回一个表,并且想要添加一个类属性。如果您使用的是StringBuilder
,则需要将该类作为参数传递。但是向 HTML 标记添加类属性不是创建表的扩展方法所关心的!如果我们切换到语义模型(TagBuilder)来生成 HTML,我们可以在表方法之外添加类属性。
除了 TagBuilder 之外,您可能还想查看 FubuMVC 的 HtmlTags 库。这是一个更好的生成 HTML 的模型。我有一些 博客上的更多详细信息。
There's a point that the other answers have missed so far. If you return
TagBuilder
from an extension method you can continue to add attributes in your view. Let's say you were returning a table from an Html helper and you want to add a class attribute. If you're using aStringBuilder
you need to pass the class in as a parameter.But adding a class attribute to an HTML tag is not the concern of an extension method that creates a table! If we switch to a semantic model (TagBuilder) for generating the HTML, we can add the class attribute outside of the table method.
In addition to TagBuilder, you might want to check out FubuMVC's HtmlTags library. It's a much better model for generating HTML. I have some more details on blog.
嗯,当然,但这不应该成为一种威慑,不是吗?一个类的设计目的比另一个类更具体,因此它提供了更大的便利性。
我可能会问:为什么使用 StringBuilder?为什么不是
List
?我不能从中生成相同的东西吗?更进一步:为什么还要使用
List
?为什么不只是一个char[]
,我可以自己控制其大小/操作?我仍然可以完全从char[]
创建一个string
。事实上,我真正需要的是一个
char*
和一个int
(长度)。正确的?我的观点是,如果一个类可用于您可以使用的专门功能,那么如果您问我,使用它是有意义的。
Well, sure, but that shouldn't be a deterrent, should it? One class is designed for something more specific than the other, so it offers a greater level of convenience.
I could ask: why use a
StringBuilder
? Why not aList<char>
? Couldn't I generate the same thing from either?Going one step further: why even a
List<char>
? Why not just achar[]
, the resizing/manipulation of which I can control myself? I can still totally create astring
from achar[]
.In fact, all I really need is a
char*
and anint
(for length). Right?My point is just that if a class is available for specialized functionality that you can use, it makes sense to use it if you ask me.
正如其他帖子中提到的,TagBuilder 带来了一些便利。
但您应该考虑到 TagBuilder 和 StringBuilder 可能不会产生相同的结果。 TagBuilder 应用 html 编码,但 StringBuilder 不应用。因此,使用TagBuilder来克服可能通过XSS攻击利用的漏洞会更安全。
As it is mentioned in the other posts, TagBuilder brings some convenience.
But you should consider that TagBuilder and StringBuilder may does not produce the same result. TagBuilder applies html encoding, but StringBuilder doesn't. So it is safer to use TagBuilder to overcome vulnerabilies that may be exploited via XSS attack.
如果您使用 StringBuilder,请不要忘记对值进行 HTML 编码。我希望 TagBuilder 自动执行此操作。
Don't forget to do HTML encoding of values if you are using StringBuilder. I hope TagBuilder do this automatically.