使用一些标签生成器或 StringBuilder 来生成对象标签是更好的做法吗? ASP.NET MVC
我编写了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我会进一步重构它,有一个方法(也许是私有的)来生成每个参数标签,减少代码重复,否则我更喜欢 StringBuilder。
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.