在 ASP.NET 中渲染服务器控件哪个更有效
我为我的服务器控件输出整个 HTML,如下所示:
public override void Render(HtmlTextWriter output)
{
output.Write(myStringBuilder.ToString());
}
myStringBuilder 是一个在单独的私有方法中手动构建的 StringBuilder 对象。
这是一种有效的方法吗?或者将 HtmlTextWriter 传递给我的私有方法并多次调用 HtmlTextWriter.Write() 更好?
I am outputting the entire HTML for my server control as follows:
public override void Render(HtmlTextWriter output)
{
output.Write(myStringBuilder.ToString());
}
myStringBuilder is a StringBuilder object that is manually built in a separate private method.
Is this an efficient way to do it? Or is it better to pass the HtmlTextWriter to my private method and make multiple calls to HtmlTextWriter.Write()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
HtmlTextWriter
传递给您的方法会更有效,然后将其写入输出流,而不是缓冲多个字符串。事实上,.Net 核心框架中的 Web 控件就是这样的。从较高的层次来看,将相同的
HtmlTextWriter
传递到所有 Render 方法中需要大量的工作。通常,当进行大量读取/写入时,处理流会更有效......这最终就是您正在做的事情(在本例中流是响应流)。免责声明:这只是一个小优化,除非您要创建单一的东西......但仍然是一个优化。
It's more efficient to pass the
HtmlTextWriter
down to your method, then it's writing to the output stream, not buffering up multiple strings.In fact, this is the way the webcontrols in the core .Net framework are. At a high level, it's a lot of passing the same
HtmlTextWriter
down into all the Render methods. Usually, when doing a lot of reading/writing, dealing with a stream is more efficient...which is ultimately what you're doing (the stream being the Reponse stream in this case).Disclaimer: This is a small optimization unless you're creating something monolithic...but an optimization none the less.