ASP.NET MVC 中的 string.Format 和 TagBuilder 之间有什么区别(如果有)?
我的 ASP.NET MVC 应用程序有一个 Html Helper 文件。它们中的大多数只是返回一个格式化的字符串。
这是我的格式化字符串助手之一的示例:
public static string Label(this HtmlHelper helper, string @for, string text)
{
return string.Format("<label for \"{0}\">{1}</label>", @for, text);
}
这是一个 TagBuilder 版本,它给了我与上面相同的结果:
public static string Label(this HtmlHelper helper, string @for, string text)
{
var builder = new TagBuilder("label");
builder.Attributes.Add("for", @for);
builder.SetInnerText(text);
return builder.ToString(TagRenderMode.Normal);
}
现在,我已经在一些网站上阅读/学习了混合实现中的 MVC。有些使用 TagBuilder
方法,有些使用 string.Format()
,有些则交替使用两者。
label 标记相当简单,因此只返回格式化字符串而不是为这样的标记实例化 TagBuilder
类会“更好”吗?
我不一定担心性能,我只是好奇为什么有些人选择 TagBuilder 而另一些人使用格式化字符串。
感谢您的启发!
I have a Html Helper file for my ASP.NET MVC application. The majority of them simply return a formatted string.
Here is an example of one of my formatted string helpers:
public static string Label(this HtmlHelper helper, string @for, string text)
{
return string.Format("<label for \"{0}\">{1}</label>", @for, text);
}
Here is a TagBuilder version that gives me the same result as above:
public static string Label(this HtmlHelper helper, string @for, string text)
{
var builder = new TagBuilder("label");
builder.Attributes.Add("for", @for);
builder.SetInnerText(text);
return builder.ToString(TagRenderMode.Normal);
}
Now, a few sites I have been reading/learning about MVC from mix up implementations. Some use the TagBuilder
method, others use string.Format()
, and some use both interchangeably.
The label tag is rather simple, so would it be 'better' to just return a formatted string rather than instantiate the TagBuilder
class for tags like this one?
I am not necessarily worried about performance, I am just curious as to why some choose TagBuilder and others use formatted strings.
Thanks for the enlightenment!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 TagBuilder 将允许您合并标签上的属性。例如,使用 String.Format,如果您想有条件地在标签上具有 CSS Class 属性,则需要执行以下操作:
而使用 TagBuilder,您可以使用 MergeAttributes() 传递键/值字典。打开 Reflector(或获取源代码)并查看 System.Web.Mvc.Html.InputHelper() 以了解使用具有大量条件属性的构建器的强大功能。
两者都可以产生相同的输出,但这实际上取决于您想要实现的目标。它还取决于您认为哪些代码是“更干净”的。
Using TagBuilder will allow you to merge attributes on the tag. For example, using String.Format, if you want to conditionally have the CSS Class attribute on a tag, you'll need to do something like:
Whereas with a TagBuilder, you can use MergeAttributes() passing in a dictionary of keys/values. Pop open Reflector (or grab the source) and look at the System.Web.Mvc.Html.InputHelper() to get an idea of the power of using a builder with a lot of conditional attributes.
Both can result in the same output, but it really depends on what you're looking to achieve. It also depends on which you consider to be "cleaner" code.
Tagbuilder 是一个便利类。它存储标签属性的字典,然后一次性输出 HTML。您也不必处理附加尖括号的问题。它本质上与您的代码执行相同的操作,如果您只有一个属性,您的方法可能同样方便。
Tagbuilder 由 HTML Helpers 在内部使用。
Tagbuilder is a convenience class. It stores a dictionary of your tag attributes, and then outputs the HTML all at once. You also don't have to deal with appending the angle brackets. It essentially does the same thing as your code is doing so, if you only have one attribute, your way might be just as convenient.
Tagbuilder is used internally by the HTML Helpers.
TagBuilder 处理属性值的 HTML 编码:SetInnerText() 编码,.InnerHTML 不编码。此外,如果您返回 TagBuilder 而不是字符串,则可以在流程稍后轻松添加 CSS 类或属性。
为什么使用 TagBuilder 而不是 StringBuilder?
TagBuilder handles the HTML encoding of attribute values: SetInnerText() encodes, .InnerHTML does not. Also, if you return a TagBuilder instead of a string you can easily add a CSS class or attribute later in the flow.
Why use TagBuilder instead of StringBuilder?