使用一些标签生成器或 StringBuilder 来生成对象标签是更好的做法吗? ASP.NET MVC

发布于 2024-09-10 12:23:09 字数 2856 浏览 10 评论 0原文

我编写了一个 HTML 帮助程序来生成 YouTube 嵌入链接。它有 3 个参数:YouTubeID、宽度和高度。我最初使用 StringBuilder 编写它,但后来我决定尝试使用 TagBuilder(好吧,其中一些)。

以下是两个不同的返回:

//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();
}

它们都生成完全相同的代码,只是 StringBuilder 在每个部分后添加了一个换行符。

有什么想法会更好,或者是否重要?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2024-09-17 12:23:09

我想我会进一步重构它,有一个方法(也许是私有的)来生成每个参数标签,减少代码重复,否则我更喜欢 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 = "&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 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); 
}

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 = "&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 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); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文