内存效率:通过codebehind传递aspx页面的Html代码

发布于 2024-08-08 05:30:12 字数 834 浏览 3 评论 0原文

我的目标是以字符串的形式生成页面的aspx代码。我通过javascript中的异步请求调用下面的代码隐藏代码,并且我通过Response.Write获取响应。

        string html = string.Empty;
        using (var memoryStream = new MemoryStream())
        {
            using (var streamWriter = new StreamWriter(memoryStream))
            {
                var htmlWriter = new HtmlTextWriter(streamWriter);
                base.Render(htmlWriter);
                htmlWriter.Flush();
                memoryStream.Position = 0;
                using (var streamReader = new StreamReader(memoryStream))
                {
                    html = streamReader.ReadToEnd();
                    streamReader.Close();
                }
            }
        }
        Response.Write(html);
        Response.End();

我想问的是上面的代码是内存效率,我正在考虑使用“yield”,因为它会延迟评估。您可以对上述代码的内存效率提出建议吗?

My goal is to generate the aspx code of a page in the form of string.I am calling the below codebehind code through asynchronous request in javascript and i am getting the response back through Response.Write

        string html = string.Empty;
        using (var memoryStream = new MemoryStream())
        {
            using (var streamWriter = new StreamWriter(memoryStream))
            {
                var htmlWriter = new HtmlTextWriter(streamWriter);
                base.Render(htmlWriter);
                htmlWriter.Flush();
                memoryStream.Position = 0;
                using (var streamReader = new StreamReader(memoryStream))
                {
                    html = streamReader.ReadToEnd();
                    streamReader.Close();
                }
            }
        }
        Response.Write(html);
        Response.End();

I want to ask that is the above code is memory efficient, I am thinking of "yield" to use as it evaluates lazily.Can u suggest on memory efficency of above code.

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

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

发布评论

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

评论(2

野侃 2024-08-15 05:30:12

使用 StringWriter 代替 MemoryStream、StreamWriter 和 StreamReader:

string html;
using (StringWriter stream = new StringWriter()) {
   using (HtmlTextWriter writer = new HtmlTextWriter(stream)) {
      base.Render(writer);
   }
   html = stream.ToString();
}
Response.Write(html);
Response.End();

StringWriter 内部使用 StringBuilder。 ToString 方法调用 Stringuilder 上的 ToString,因此它返回内部字符串缓冲区作为字符串。这意味着该字符串仅创建一次,并且不会来回复制。

Use a StringWriter instead of the MemoryStream, the StreamWriter and the StreamReader:

string html;
using (StringWriter stream = new StringWriter()) {
   using (HtmlTextWriter writer = new HtmlTextWriter(stream)) {
      base.Render(writer);
   }
   html = stream.ToString();
}
Response.Write(html);
Response.End();

The StringWriter uses a StringBuilder internally. The ToString method calls ToString on the Stringuilder, so it returns the internal string buffer as the string. That means that the string is only created once, and not copied back and forth.

阳光的暖冬 2024-08-15 05:30:12

您的方法将一个 html 副本存储在 html 变量中,并将另一个副本存储在 memoryStream 中。试试这个:

base.Render(new HtmlTextWriter(Response.Output));
Response.End();

虽然这可行,但我不确定你想实现什么目标。

Your method stores an html copy at html variable, and another at memoryStream. Try this:

base.Render(new HtmlTextWriter(Response.Output));
Response.End();

While this can work, I'm not sure what are you trying to accomplish.

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