Anders Lindén
Anders Lindén

Reputation: 7303

Can HttpServerUtility.Execute get binary data?

This is how I normally execute an aspx web form and get a string from the output:

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

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

    html = sw.ToString();
  }

  return html;
}

How do I instead get a byte array?

Upvotes: 0

Views: 77

Answers (1)

Tommy Nguyen
Tommy Nguyen

Reputation: 116

The suggestion is from this post https://stackoverflow.com/a/1745456/3646986. Use the StreamWriter instead of the StringWriter

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

Upvotes: 0

Related Questions