views:

321

answers:

1

So, I'm playing around with getting the HTML out of a standard ASP.Net Page. I've overridden the Render method, as so:

protected override void Render(HtmlTextWriter writer)
{
    MemoryStream memoryStream = new MemoryStream();

    try
    {
        using (StreamWriter streamWriter = new StreamWriter(memoryStream))
        {
            var textWriter = new HtmlTextWriter(streamWriter);
            base.Render(textWriter);
            memoryStream.Position = 0;
            using (StreamReader reader = new StreamReader(memoryStream))
            {
                var text = reader.ReadToEnd();
                Response.Write(text);
                reader.Close();
            }
        }
    }
    catch(ObjectDisposedException)
    {
        // The stream writer is already disposed?
    }
    finally
    {
        memoryStream.Dispose();
    }
}

This works great on files that are less than 8.00 KB (8,200 bytes). If the file is larger than that, any text at the end is being cut off.

Does anyone have an suggestions?

+1  A: 

Maybe, textWriter.Flush() after a call to base.Render() will save your day.

Anton Gogolev
That took care of it. I'm not sure _why_ it took care of it, but it did, and that's the important thing. </cargoCulting>
Matt Grande