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

雨燕双飞 提交于 2019-12-20 17:45:48

问题


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>");

回答1:


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




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/573722/are-there-any-benefits-to-using-htmltextwriter-if-you-are-not-going-to-benefit-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!