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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/573722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:48:19  来源:igfitidea点击:

Are there any benefits to using HtmlTextWriter if you are not going to benefit from adaptive rendering?

c#streamconventionshtmltextwriter

提问by Chris Ballance

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:

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

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

采纳答案by tsimon

I can think of two reasons to use HtmlTextWriter:

我可以想到使用 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).

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

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

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 :-).

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

回答by baretta

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.

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

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).

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

回答by cgreeno

HtmlTextWriter is beneficial because:

HtmlTextWriter 是有益的,因为:

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

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

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.

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

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

这里有一个关于为什么要使用 HtmlTextWriter 来保存和重用 HTML 输出的示例。

回答by Jan Zich

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:

另一个优点可能是使用 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();

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).

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

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.

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

回答by Dan Esparza

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

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

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

XSS是真实存在的,您可以做的任何事情都可以让未来的开发人员更轻松地维护您的代码。