Why use TagBuilder instead of StringBuilder?

这一生的挚爱 提交于 2019-11-27 11:28:50

问题


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??


回答1:


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 use StringBuilder and the result will be the same, but you can do things easier with TagBuilder. Lets say you need to generate a tag:

<a href='http://www.stackoverflow.com' class='coolLink'/>

Using StringBuilder you need to write something like this:

var sb = new StringBuilder();
sb.Append("<a href='");
sb.Append(link);
sb.Append("' class = '");
sb.Append(ccsClass);
sb.Append("'/>");
sb.ToString();

It is not very cool, isn’t it? And compare how you can build it using TagBuilder;

var tb = new TagBuilder("a");
tb.MergeAttribute("href",link);
tb.AddCssClass(cssClass);
tb.ToString(TagRenderMode.SelfClosing);

Isn't that better?




回答2:


It's just convenience. From this tutorial:

You don’t really need to use the TagBuilder class. You could use a StringBuilder class instead. However, the TagBuilder class makes your life a little easier.

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?




回答3:


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 a StringBuilder you need to pass the class in as a parameter.

public static string Table(...., string @class)
{
    ...
    sb.AppendFormat("class='{0}", @class);
    ...
}

// In the view
<%: Html.Table(someParams, "fancy") %>

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.

public static TagBuilder Table(....)
{
    ...
    return tag;
}

// In the view
<%: Html.Table(someParams).AddCssClass("fancy") %>

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.




回答4:


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 a List<char>? Couldn't I generate the same thing from either?

Going one step further: why even a List<char>? Why not just a char[], the resizing/manipulation of which I can control myself? I can still totally create a string from a char[].

In fact, all I really need is a char* and an int (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.




回答5:


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.




回答6:


Don't forget to do HTML encoding of values if you are using StringBuilder. I hope TagBuilder do this automatically.



来源:https://stackoverflow.com/questions/3043654/why-use-tagbuilder-instead-of-stringbuilder

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