what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable?
使用标记构建器和字符串构建器在htmlhelper类中创建表或使用HtmlTable有什么不同?
aren't they generating the same thing??
它们产生的不是一样的东西吗?
6 个解决方案
#1
61
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:
TagBuilder是专门为创建html标记及其内容而设计的类。你说的对,结果将是一个字符串当然你仍然可以使用StringBuilder结果是一样的,但是你可以用TagBuilder来做更简单的事情。假设您需要生成一个标签:
<a href='http://www.*.com' class='coolLink'/>
Using StringBuilder
you need to write something like this:
使用StringBuilder时,您需要编写如下内容:
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
;
这不是很酷,不是吗?并比较如何使用TagBuilder来构建它;
var tb = new TagBuilder("a");
tb.MergeAttribute("href",link);
tb.AddCssClass(cssClass);
tb.ToString(TagRenderMode.SelfClosing);
Isn't that better?
那不是更好吗?
#2
15
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.
您实际上并不需要使用TagBuilder类。您可以使用StringBuilder类。然而,TagBuilder类使您的生活更轻松。
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的方法,并考虑它们是否会给您带来价值。您是否希望每次都在StringBuilder中手工做同样的事情?它对你有帮助吗?属性合并,等等?生成的代码是否更易于阅读,更清楚地表明您正在构建一个标记,而不是某个任意字符串?
#3
12
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.
到目前为止,其他的答案都忽略了一点。如果从扩展方法返回TagBuilder,则可以继续在视图中添加属性。假设您正在从一个Html助手返回一个表,您想要添加一个class属性。如果使用StringBuilder,则需要将类作为参数传递。
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.
但是向HTML标记添加类属性不是创建表的扩展方法所关心的!如果我们切换到语义模型(TagBuilder)来生成HTML,我们可以在表方法之外添加class属性。
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.
除了TagBuilder之外,您可能还需要查看FubuMVC的HtmlTags库。它是生成HTML更好的模型。我有更多关于博客的细节。
#4
8
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?
我可能会问:为什么要使用StringBuilder?为什么不一个列表< char > ?我不能从两者中产生同样的东西吗?
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?
实际上,我真正需要的是一个char*和一个int(表示长度)。对吧?
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
1
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.
正如其他文章中提到的,TagBuilder为我们带来了一些便利。但是您应该考虑到TagBuilder和StringBuilder可能不会产生相同的结果。TagBuilder应用html编码,但StringBuilder没有。因此,使用TagBuilder来克服可能被XSS攻击利用的漏洞更安全。
#6
0
Don't forget to do HTML encoding of values if you are using StringBuilder. I hope TagBuilder do this automatically.
如果使用StringBuilder,不要忘记对值进行HTML编码。我希望TagBuilder能自动完成这个任务。
#1
61
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:
TagBuilder是专门为创建html标记及其内容而设计的类。你说的对,结果将是一个字符串当然你仍然可以使用StringBuilder结果是一样的,但是你可以用TagBuilder来做更简单的事情。假设您需要生成一个标签:
<a href='http://www.*.com' class='coolLink'/>
Using StringBuilder
you need to write something like this:
使用StringBuilder时,您需要编写如下内容:
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
;
这不是很酷,不是吗?并比较如何使用TagBuilder来构建它;
var tb = new TagBuilder("a");
tb.MergeAttribute("href",link);
tb.AddCssClass(cssClass);
tb.ToString(TagRenderMode.SelfClosing);
Isn't that better?
那不是更好吗?
#2
15
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.
您实际上并不需要使用TagBuilder类。您可以使用StringBuilder类。然而,TagBuilder类使您的生活更轻松。
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的方法,并考虑它们是否会给您带来价值。您是否希望每次都在StringBuilder中手工做同样的事情?它对你有帮助吗?属性合并,等等?生成的代码是否更易于阅读,更清楚地表明您正在构建一个标记,而不是某个任意字符串?
#3
12
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.
到目前为止,其他的答案都忽略了一点。如果从扩展方法返回TagBuilder,则可以继续在视图中添加属性。假设您正在从一个Html助手返回一个表,您想要添加一个class属性。如果使用StringBuilder,则需要将类作为参数传递。
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.
但是向HTML标记添加类属性不是创建表的扩展方法所关心的!如果我们切换到语义模型(TagBuilder)来生成HTML,我们可以在表方法之外添加class属性。
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.
除了TagBuilder之外,您可能还需要查看FubuMVC的HtmlTags库。它是生成HTML更好的模型。我有更多关于博客的细节。
#4
8
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?
我可能会问:为什么要使用StringBuilder?为什么不一个列表< char > ?我不能从两者中产生同样的东西吗?
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?
实际上,我真正需要的是一个char*和一个int(表示长度)。对吧?
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
1
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.
正如其他文章中提到的,TagBuilder为我们带来了一些便利。但是您应该考虑到TagBuilder和StringBuilder可能不会产生相同的结果。TagBuilder应用html编码,但StringBuilder没有。因此,使用TagBuilder来克服可能被XSS攻击利用的漏洞更安全。
#6
0
Don't forget to do HTML encoding of values if you are using StringBuilder. I hope TagBuilder do this automatically.
如果使用StringBuilder,不要忘记对值进行HTML编码。我希望TagBuilder能自动完成这个任务。