简体   繁体   English

ASP.NET MVC中string.Format和TagBuilder之间有什么区别?

[英]What is the difference, if any, between string.Format and TagBuilder in ASP.NET MVC?

I have a Html Helper file for my ASP.NET MVC application. 我的ASP.NET MVC应用程序有一个Html Helper文件。 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: 这是一个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);
}

Now, a few sites I have been reading/learning about MVC from mix up implementations. 现在,我已经从混合实现中读取/学习MVC的一些站点。 Some use the TagBuilder method, others use string.Format() , and some use both interchangeably. 有些使用TagBuilder方法,有些使用string.Format() ,有些则互换使用。

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? 标签标签相当简单,那么返回格式化的字符串而不是像这样的标签实例化TagBuilder类会“更好”吗?

I am not necessarily worried about performance, I am just curious as to why some choose TagBuilder and others use formatted strings. 我不一定担心性能,我只是好奇为什么有些人选择TagBuilder而其他人使用格式化的字符串。

Thanks for the enlightenment! 谢谢你的启示!

Using TagBuilder will allow you to merge attributes on the tag. 使用TagBuilder将允许您合并标签上的属性。 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: 例如,使用String.Format,如果要在标记上有条件地具有CSS Class属性,则需要执行以下操作:

String.Format("<label {0}>Text</label>", (includeClass ? "class=\"Something\"" : ""));

Whereas with a TagBuilder, you can use MergeAttributes() passing in a dictionary of keys/values. 而使用TagBuilder,您可以使用MergeAttributes()传递键/值的字典。 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. 弹出打开Reflector(或抓取源代码)并查看System.Web.Mvc.Html.InputHelper()以了解使用具有大量条件属性的构建器的强大功能。

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 is a convenience class. Tagbuilder是一个方便的类。 It stores a dictionary of your tag attributes, and then outputs the HTML all at once. 它存储标签属性的字典,然后一次输出所有HTML。 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. HTML助手在内部使用Tagbuilder。

TagBuilder handles the HTML encoding of attribute values: SetInnerText() encodes, .InnerHTML does not. TagBuilder处理属性值的HTML编码:SetInnerText()编码,.InnerHTML不编码。 Also, if you return a TagBuilder instead of a string you can easily add a CSS class or attribute later in the flow. 此外,如果您返回TagBuilder而不是字符串,则可以在流程中稍后轻松添加CSS类或属性。

Why use TagBuilder instead of StringBuilder? 为什么使用TagBuilder而不是StringBuilder?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM