Hello SO:
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!