您认为哪种方法输出动态 HTML 更具可读性?
我想这将取决于个人喜好,但你会走哪条路呢?
StringBuilder markup = new StringBuilder();
foreach (SearchResult image in Search.GetImages(componentId))
{
markup.Append(String.Format("<div class=\"captionedImage\"><img src=\"{0}\" width=\"150\" alt=\"{1}\"/><p>{1}</p></div>", image.Resolutions[0].Uri, image.Caption));
}
LiteralMarkup.Text = markup.ToString();
对比。
StringBuilder markup = new StringBuilder();
foreach (SearchResult image in Search.GetImages(componentId))
{
markup.Append(String.Format(@"<div class=""captionedImage""><img src=""{0}"" width=""150"" alt=""{1}""/><p>{1}</p></div>", image.Resolutions[0].Uri, image.Caption));
}
LiteralMarkup.Text = markup.ToString();
或者我根本不应该这样做并使用 HtmlTextWriter 类来代替?
编辑:这里有一些非常好的建议。 我们使用 2.0 框架,因此 LINQ 不可用
I'm imagining that this will come down to personal preference but which way would you go?
StringBuilder markup = new StringBuilder();
foreach (SearchResult image in Search.GetImages(componentId))
{
markup.Append(String.Format("<div class=\"captionedImage\"><img src=\"{0}\" width=\"150\" alt=\"{1}\"/><p>{1}</p></div>", image.Resolutions[0].Uri, image.Caption));
}
LiteralMarkup.Text = markup.ToString();
Vs.
StringBuilder markup = new StringBuilder();
foreach (SearchResult image in Search.GetImages(componentId))
{
markup.Append(String.Format(@"<div class=""captionedImage""><img src=""{0}"" width=""150"" alt=""{1}""/><p>{1}</p></div>", image.Resolutions[0].Uri, image.Caption));
}
LiteralMarkup.Text = markup.ToString();
Or should I not be doing this at all and using the HtmlTextWriter class instead?
EDIT: Some really good suggestions here. We are on 2.0 framework so LINQ is not available
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一次投票给“AppendFormat”。 另外,为了服务器代码的缘故,我可能会在此处使用单引号,以避免需要转义任何内容:
最后,您可能还需要在某处进行额外的检查以防止 html/xss 注入。
另一种选择是将图像封装在一个类中:
这样做的优点是可以轻松地重复使用并随着时间的推移向概念添加功能。 为了获得真正的奇特效果,您可以将该类转换为用户控件。
Another vote for "AppendFormat". Also, for the sake of the server code I might put up with single quotes here to avoid the need to escape anything:
Finally, you may also want an additional check in there somewhere to prevent html/xss injection.
Another option is to encapsulate your image in a class:
This has the advantage of making it easy to re-use and add features to the concept over time. To get real fancy you can turn that class into a usercontrol.
我个人会选择第一个选项。 还想指出的是,这里有一个快速代码保存技巧。 您可以将 AppendFormat 用于 StringBuilder。
编辑:HtmlTextWriter 方法将为您提供更加结构化的结果,如果要生成的 HTML 数量增加,那么这将是一个明显的选择。 或者使用 HTML 文件并将其用作模板,也许还有字符串替换
ASP.NET 用户控件是模板化的另一个不错的选择。
Me personally I would opt for the first option. Just to point out also, a quick code saving tip here. you can use AppendFormat for the StringBuilder.
EDIT: The HtmlTextWriter approach would give you a much more structured result, and if the amount of HTML to generate grew, then this would be an obvious choice. OR the use of an HTML file and using it as a template and maybe string replacements
ASP.NET User Control are another good choice for templating.
我更喜欢:
正如另一个答案中提到的,使用 AppendFormat()。
看看 SharpTemplate 的 html 模板,它使得即使是少量的内容也更容易阅读HTML 的。
I'd prefer:
As mentioned in another answer, using AppendFormat().
Take a look at SharpTemplate for html templating, it makes it all a lot easier to read even for small amounts of HTML.
我更喜欢在 aspx 中使用 ListView 控件来执行此操作,该控件正是为此目的而制作的。 因此,您的代码将保持可读性并且是独立的(C# 代码中没有 HTML)。 使用您的方法,您将不会收到编译时 XHTML 验证警告。
I would prefer doing this in aspx using the ListView Control which exactly is made for this purpose. So your code will remain readable and is seperated (no HTML in the C# Code). With your approach you will get no compilte time XHTML validation warnings.
我创建了一些帮助程序类来创建 html 控件,因此我的代码如下所示:
起初它可能看起来并不简单,但它很容易使用,创建对值进行正确 html 编码的控件,并且没有字符串逃避问题。
I have made some helper classes to create html controls, so my code would look like this:
At first it might not look simpler, but it's easy to work with, creates controls that does proper html encoding of the values, and there is no string escaping issues.