I have an Html Helper file for my ASP.NET MVC application. Most of them just 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 the version of TagBuilder 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 that I have read / learned about MVC from mixed implementations. Some use the TagBuilder method, others use string.Format() , and some use interchangeably.
The tag tag is pretty simple, so it would be βbetterβ just to return a formatted string instead of instantiating the TagBuilder class for tags like this?
I'm not necessarily worried about performance, I'm just wondering why some choose TagBuilder and others use formatted strings.
Thanks for the enlightenment!
source share