为什么使用 TagBuilder 而不是 StringBuilder?

发布于 2024-09-05 21:13:20 字数 81 浏览 10 评论 0原文

使用标签生成器和字符串生成器在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

睫毛上残留的泪 2024-09-12 21:13:20

TagBuilder 是专门为创建 html 标签及其内容而设计的类。你说得对,结果无论如何都是一个字符串,当然你仍然可以使用 StringBuilder 并且结果将是相同的,但是你可以使用 TagBuilder 做得更容易。假设您需要生成一个标签:

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

使用 StringBuilder 您需要编写如下内容:

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

这不是很酷,不是吗?
并比较如何使用 TagBuilder 构建它;

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

那不是更好吗?

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?

小傻瓜 2024-09-12 21:13:20

这只是方便而已。来自本教程 :

您实际上并不需要使用 TagBuilder 类。
您可以使用 StringBuilder 类
反而。但是,TagBuilder 类
让您的生活变得更轻松。

查看 TagBuilder 上的方法,并考虑它们是否给您带来价值。您是否希望每次都在 StringBuilder 中手动执行相同的操作?它对你来说有逃避的意义吗?属性合并等等?生成的代码是否更易于阅读,是否更清楚地表明您正在构建标签而不是某个任意字符串?

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?

如日中天 2024-09-12 21:13:20

到目前为止,其他答案都忽略了一点。如果您从扩展方法返回 TagBuilder,您可以继续在视图中添加属性。假设您从 Html 助手返回一个表,并且想要添加一个类属性。如果您使用的是StringBuilder,则需要将该类作为参数传递。

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

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

但是向 HTML 标记添加类属性不是创建表的扩展方法所关心的!如果我们切换到语义模型(TagBuilder)来生成 HTML,我们可以在表方法之外添加类属性。

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

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

除了 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 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.

眼角的笑意。 2024-09-12 21:13:20

它们不是生成相同的东西吗?

嗯,当然,但这不应该成为一种威慑,不是吗?一个类的设计目的比另一个类更具体,因此它提供了更大的便利性。

我可能会问:为什么使用 StringBuilder?为什么不是 List?我不能从中生成相同的东西吗?

更进一步:为什么还要使用 List?为什么不只是一个 char[],我可以自己控制其大小/操作?我仍然可以完全从 char[] 创建一个string

事实上,我真正需要的是一个 char* 和一个 int (长度)。正确的?

我的观点是,如果一个类可用于您可以使用的专门功能,那么如果您问我,使用它是有意义的。

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.

嗼ふ静 2024-09-12 21:13:20

正如其他帖子中提到的,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.

暖伴 2024-09-12 21:13:20

如果您使用 StringBuilder,请不要忘记对值进行 HTML 编码。我希望 TagBuilder 自动执行此操作。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文