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??
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??
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.
Don't forget to do HTML encoding of values if you are using StringBuilder. I hope TagBuilder do this automatically.
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.
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
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?
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.