如果您不打算从自适应渲染中受益,那么使用 HtmlTextWriter 有什么好处吗?

发布于 2024-07-14 09:13:22 字数 548 浏览 12 评论 0原文

除了从替代设备的自适应渲染中受益之外,编写所有这些代码是否有意义:

writer.WriteBeginTag("table");
writer.WriteBeginTag("tr");
writer.WriteBeginTag("td");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEncodedText(someTextVariable);
writer.WriteEndTag("td");
writer.WriteEndTag("tr");
writer.WriteEndTag("table");

当 StringBuilder 可以简单地构建相同的东西时:

sb.Append("<table><tr><td>");
sb.Append(someTextVariable);
sb.Append("</td></tr></table>");

Outside of benefiting from Adaptive Rendering for alternate devices, does it ever make sense to write all of this code:

writer.WriteBeginTag("table");
writer.WriteBeginTag("tr");
writer.WriteBeginTag("td");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEncodedText(someTextVariable);
writer.WriteEndTag("td");
writer.WriteEndTag("tr");
writer.WriteEndTag("table");

When StringBuilder could build the same thing with simply this:

sb.Append("<table><tr><td>");
sb.Append(someTextVariable);
sb.Append("</td></tr></table>");

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

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

发布评论

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

评论(5

踏雪无痕 2024-07-21 09:13:22

另一个优点可能是使用 HtmlTextWriter 可以以更干净(更易于维护)的方式格式化代码,并且 HtmlTextWriter 支持自动编码 HTML。 比较:

writer.AddAttribute(HtmlTextWriterAttribute.Id, "someId");
if (!string.IsNullOrEmpty(cssClass)) writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.WriteEncodedText(text);
writer.RenderEndTag();

与:

StringBuilder html = new StringBuilder();
html.Append("<span");
html.Append(" id=\"someId\"");
if (!string.IsNullOrEmpty(cssClass)) html.AppendFormat(" class=\"{0}\"", HttpUtility.HtmlAttributeEncode(cssClass));
html.Append(">");
html.Append(HttpUtility.HtmlEncode(text));
html.Append("</span>");

有人可能会认为第二个示例中的代码可以用一种不同的、可能更干净的方式编写,但这可以被视为 HtmlTextWriter 的一个优势,因为它基本上强制执行一种规范的格式化方式(这再次提高了维护性) )。

编辑:事实上,我在第二个片段中犯了一个错误,我需要返回并修复响应。 这证实了我想表达的观点。

Another advantage could be that using HtmlTextWriter one could format code in a cleaner (more maintenance friendly) way, and that HtmlTextWriter supports encoding HTML automatically. Compare:

writer.AddAttribute(HtmlTextWriterAttribute.Id, "someId");
if (!string.IsNullOrEmpty(cssClass)) writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.WriteEncodedText(text);
writer.RenderEndTag();

versus:

StringBuilder html = new StringBuilder();
html.Append("<span");
html.Append(" id=\"someId\"");
if (!string.IsNullOrEmpty(cssClass)) html.AppendFormat(" class=\"{0}\"", HttpUtility.HtmlAttributeEncode(cssClass));
html.Append(">");
html.Append(HttpUtility.HtmlEncode(text));
html.Append("</span>");

One may argue that the code in the second example can be written in a different, possibly cleaner, way, but this could be seen as an advantage of HtmlTextWriter because it basically enforces one canonical way of formatting (which again improves maintenance).

Edit: In fact, I actually made a mistake in the second snippet, and I needed to go back and fix the response. This confirms the point I wanted to make.

不顾 2024-07-21 09:13:22

我可以想到使用 HtmlTextWriter 的两个原因:

  1. 您可以使用编写器来跟踪缩进,以便输出的 HTML 格式良好,而不是出现在一行中

  2. HtmlTextWriter 通常与输出流关联,因此它应该比在内存中构建长字符串更有效(取决于 。

这些都不是特别的原因,但它们足以说服我在需要效率时使用编写器,或者如果我正在编写一个将被重用并且应该尽可能专业的基本控件。 你的旅费可能会改变 :-)。

I can think of two reasons to use HtmlTextWriter:

  1. You can use the writer to keep track of your indents, so that your outputted HTML is formatted nicely, rather than appearing on one line

  2. HtmlTextWriter is usually associated with an output stream, so it should be more efficient than building up a long string in memory (depending upon how much HTML you are generating).

Neither of these are extraordinary reasons, but they are enough to convince me to use the writer when efficiency is needed, or if I am writing a base control that will be reused and should be as professional as possible. Your mileage may vary :-).

别挽留 2024-07-21 09:13:22

HtmlTextWriter 是有益的,因为:

HtmlTextWriter 是最干净的,并且标记在呈现时缩进得很好。

HtmlTextWriter 直接写入输出流会对性能产生影响。 在调用 ToString 之前,Stringbuilder 不会写入输出流。

有一个示例说明为什么要使用 HtmlTextWriter

HtmlTextWriter is beneficial because:

HtmlTextWriter is the cleanest and the mark-up is nicely indented when it is rendered.

There is a performance impact as HtmlTextWriter writes directly to the output stream. Stringbuilder doesn't write to the output stream until ToString is called on it.

There is an example on why you would use HtmlTextWriter for Saving and Reusing HTML Output here as well.

岁月打碎记忆 2024-07-21 09:13:22

在某些时候,您仍然需要将其传递给 HtmlTextWriter 以便呈现给客户端。 我猜你会有一个最终的 writer.Write(sb.ToString()); 在第二个例子中。
您可以减少代码行数的方法是编写原始 HTML,与第二个 StringBuilder 示例中的方式完全相同,但使用 HtmlTextWriter.Write 代替。

writer.Write("<table><tr><td>");
writer.Write(someTextVariable);
writer.Write("</td></tr></table>");

那么使用 StringBuilder 似乎没有必要。
并且,HtmlTextWriter 至少在某种程度上将确保生成的 HTML 是兼容的(尽管在编写原始 HTML 时,上述情况并非如此)。

At some point, you still need to pass this to a HtmlTextWriter in order to render to the client. I guess you would have a final writer.Write(sb.ToString()); in there in the second example.
What you can do to reduce the lines of code is writing raw HTML, exactly the same way as in your second StringBuilder example, but using HtmlTextWriter.Write instead.

writer.Write("<table><tr><td>");
writer.Write(someTextVariable);
writer.Write("</td></tr></table>");

Then using a StringBuilder seems unnecessary.
And, HtmlTextWriter will, at least to some extent, make sure the generated HTML is compliant(although that is not true in the above case, when writing raw HTML).

独孤求败 2024-07-21 09:13:22

我能想到的最大原因是避免采取额外的步骤来清理您的输入。 根据文档, WriteEncodedText 将自动适当地设置任何尖括号的格式。

XSS 是真实存在的,您可以做任何事情来使未来的开发人员更容易维护代码是一个好处。

The biggest reason I can think of is to avoid having to take an extra step to sanitize your input. According to the docs, WriteEncodedText will automatically format any angle brackets appropriately.

XSS is a real thing, and anything you can do to make it easier for future devs to maintain your code is a benefit.

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