Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 390633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:59:01+00:00 2026-05-12T15:59:01+00:00

I have a Html Helper file for my ASP.NET MVC application. The majority of

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:59:01+00:00Added an answer on May 12, 2026 at 3:59 pm

    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:

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have extended the ASP.NET MVC Html Helper to include my own ValidationImage that
i have this code in one of my asp.net mvc views: <%Html.RenderFile(@C:\Members\newsletters\welcome.html);%> I have
I have an extension method for getting a string from resource file in asp.net
Does anyone know how to use the new ASP.Net MVC 3 Html Helper WebImage
Using ASP.NET MVC & Spark, I have a view that is listing a number
I have been using telerik extensions for an asp.net mvc project, more specifically the
I have converted my classic asp.net mvc views to razor. In a view there
I have a small helper app that I use to inject scripts into html
Recently I've read, and implemented 80% of examples from Pro ASP.NET MVC Framework ,
I'm working through the ASP.NET MVC article at http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx . (Note: Some of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.