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 940933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:02:17+00:00 2026-05-15T22:02:17+00:00

I wrote an HTML helper to generate a YouTube embed link. It has 3

  • 0

I wrote an HTML helper to generate a YouTube embed link. It has 3 parameters, YouTubeID, Width, and Height. I originally wrote it with a StringBuilder, but then I decided to try to use the TagBuilder (well, a few of them).

Here are the two different returns:

//Tag Builder
public static string YouTube(this HtmlHelper helper, string youtubeId, string width, string height)
{
    const string vidSuffix = "&hl=en_US&fs=1";
    var url = "http://www.youtube.com/v/" + youtubeId + vidSuffix;

    var objBuilder = new TagBuilder("object");
    objBuilder.MergeAttribute("width",width);
    objBuilder.MergeAttribute("height",height);

    var movParamBuilder = new TagBuilder("param");
    movParamBuilder.MergeAttribute("name","movie");
    movParamBuilder.MergeAttribute("value",url);

    var fsParamBuilder = new TagBuilder("param");
    fsParamBuilder.MergeAttribute("name","allowFullScreen");
    fsParamBuilder.MergeAttribute("value","true");

    var saParamBuilder = new TagBuilder("param");
    saParamBuilder.MergeAttribute("name", "allowscriptaccess");
    saParamBuilder.MergeAttribute("value", "always");

    var embedBuilder = new TagBuilder("embed");
    embedBuilder.MergeAttribute("src",url);
    embedBuilder.MergeAttribute("type", "application/x-shockwave-flash");
    embedBuilder.MergeAttribute("allowscriptaccess","always");
    embedBuilder.MergeAttribute("allowfullscreen","true");
    embedBuilder.MergeAttribute("width",width);
    embedBuilder.MergeAttribute("height",height);

    objBuilder.InnerHtml = movParamBuilder.ToString(TagRenderMode.Normal) +
                           fsParamBuilder.ToString(TagRenderMode.Normal) +
                           saParamBuilder.ToString(TagRenderMode.Normal) +
                           embedBuilder.ToString(TagRenderMode.Normal);

    return objBuilder.ToString(TagRenderMode.Normal);
}


//StringBuilder
public static string YouTube(this HtmlHelper helper, string youtubeId, string width, string height)
{
    const string vidSuffix = "&hl=en_US&fs=1";
    var url = "http://www.youtube.com/v/" + youtubeId + vidSuffix;

    sb.AppendFormat("<object width=\"{0}\" height=\"{1}\">", width, height);
    sb.AppendLine();
    sb.AppendFormat("<param name=\"movie\" value=\"{0}\">",url);
    sb.AppendLine();
    sb.Append("</param><param name=\"allowFullScreen\" value=\"true\">");
    sb.AppendLine();
    sb.AppendFormat("</param><param name=\"allowscriptaccess\" value=\"always\">");
    sb.AppendLine();
    sb.AppendFormat(
        "</param><embed src=\"{0}\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"{1}\" height=\"{2}\">",
        url, width, height);
    sb.AppendLine();
    sb.Append("</embed></object>");

    return sb.ToString();
}

Both of them generate the exact same code, except the StringBuilder adds a line break after each section.

Any thoughts on which would be better, or if it even matters? Thanks!

  • 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-15T22:02:18+00:00Added an answer on May 15, 2026 at 10:02 pm

    I think I would refactor it even more an have a method, perhaps private, to produce each of the parameter tags, reducing the code duplication, otherwise I prefer the StringBuilder.

    private static string Param( this HtmlHelper helper, string name, string value )
    {
        var tagBuilder = new TagBuilder("param"); 
        tagBuilder .MergeAttribute("name", name); 
        tagBuilder .MergeAttribute("value", value);
        return tagBuilder.ToString( TagRenderMode.Normal );
    }
    
    public static string YouTube(this HtmlHelper helper, string youtubeId, string width, string height) 
    { 
        const string vidSuffix = "&amp;hl=en_US&amp;fs=1"; 
        var url = "http://www.youtube.com/v/" + youtubeId + vidSuffix; 
    
        var objBuilder = new TagBuilder("object"); 
        objBuilder.MergeAttribute("width",width); 
        objBuilder.MergeAttribute("height",height);  
    
        var embedBuilder = new TagBuilder("embed"); 
        embedBuilder.MergeAttribute("src",url); 
        embedBuilder.MergeAttribute("type", "application/x-shockwave-flash"); 
        embedBuilder.MergeAttribute("allowscriptaccess","always"); 
        embedBuilder.MergeAttribute("allowfullscreen","true"); 
        embedBuilder.MergeAttribute("width",width); 
        embedBuilder.MergeAttribute("height",height); 
    
        objBuilder.InnerHtml = helper.Param( "movie", url ) + 
                               helper.Param( "allowFullScreen", "true" ) + 
                               helper.Param( "allowscriptaccess", "always" ) + 
                               embedBuilder.ToString(TagRenderMode.Normal); 
    
        return objBuilder.ToString(TagRenderMode.Normal); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a regex to fetch string from HTML, but it seems the multiline
error with url helper, $this->url(array(), 'home'); generate a link like this http///home Hi, I've
I wrote HTML to create the content shown in the image below on Safari
I've installed Tomcat and I've been testing it: I wrote some .html and .jsp
I followed this tutorial http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html I wrote the manifest File giving all the permissions
I wrote vbscript inside my html file for my site and I can't get
I wrote the following: <html> <head> </head> <body> <input type=date /> </body> </html> Just
I wrote a simple webpage as follows: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN
I wrote a sliding list for a project. Here it is: http://study-wise.appspot.com/test/left_right.html I have
I'm having a good old play with Redis Webdis Dart I wrote this #import('dart:html');

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.