HttpServerUtility.Execute可以获取二进制数据吗?

时间:2017-02-21 13:40:24

标签: asp.net httpcontext httpserverutility

这就是我通常执行aspx Web表单并从输出中获取字符串的方式:

public static string GetAspPageOutput(string page)
{
  string html;

  using (var sw = new StringWriter())
  {
    HttpContext.Current.Server.Execute(page, sw);

    html = sw.ToString();
  }

  return html;
}

我如何获得字节数组呢?

1 个答案:

答案 0 :(得分:0)

建议来自这篇文章https://stackoverflow.com/a/1745456/3646986。使用StreamWriter而不是StringWriter

MemoryStream ms = new MemoryStream();
StreamWriter writer = new StreamWriter(ms);
context.Server.Execute(virtualpath, writer);
var bytes = ms.toArray()
return bytes