0

How to capture the dynamic HTML content from ASP.NET button click event?

The same functionality for ASP as well.

Please guide me on this.

subramani
  • 1,039
  • 3
  • 13
  • 22

1 Answers1

0

Please check below link also : http://www.west-wind.com/weblog/posts/2004/Jun/08/Capturing-Output-from-ASPNet-Pages

Good question, i had to try out and see if i could create a HttpModule to do what you are describing.

I didnt have any luck trying to read from the responsestream, but using the ResponseFilter gave me a way to capture the content.

The following code seems to work pretty good, and i figured maybe you could use the code as a base. But remember this is just something i threw together fast, it has not been tested in any way. So dont use it in any production environment without proper reviewing/testing and such. Feel free to comment on it though ;)

public class ResponseLoggerModule : IHttpModule
{
    private class ResponseCaptureStream : Stream
    {
        private readonly Stream _streamToCapture;
        private readonly Encoding _responseEncoding;

        private string _streamContent;
        public string StreamContent
        {
            get { return _streamContent; }
            private set
            {
                _streamContent = value;
            }
        }

        public ResponseCaptureStream(Stream streamToCapture, Encoding responseEncoding)
        {
            _responseEncoding = responseEncoding;
            _streamToCapture = streamToCapture;

        }

        public override bool CanRead
        {
            get { return _streamToCapture.CanRead; }
        }

        public override bool CanSeek
        {
            get { return _streamToCapture.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return _streamToCapture.CanWrite; }
        }

        public override void Flush()
        {
            _streamToCapture.Flush();
        }

        public override long Length
        {
            get { return _streamToCapture.Length; }
        }

        public override long Position
        {
            get
            {
                return _streamToCapture.Position;
            }
            set
            {
                _streamToCapture.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _streamToCapture.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _streamToCapture.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _streamToCapture.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            _streamContent += _responseEncoding.GetString(buffer);
            _streamToCapture.Write(buffer, offset, count);
        }

        public override void Close()
        {
            _streamToCapture.Close();
            base.Close();
        }
    }

    #region IHttpModule Members

    private HttpApplication _context;
    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        _context = context;

        context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
    }

    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        _context.Response.Filter = new ResponseCaptureStream(_context.Response.Filter, _context.Response.ContentEncoding);
    }

    void context_PreSendRequestContent(object sender, EventArgs e)
    {
        ResponseCaptureStream filter = _context.Response.Filter as ResponseCaptureStream;

        if (filter != null)
        {
            string responseText = filter.StreamContent;

            // Logging logic here
        }
    }

    #endregion
}

Reference Question : Capturing HTML generated from ASP.NET

Community
  • 1
  • 1
sikender
  • 5,883
  • 7
  • 42
  • 80
  • Is it possile to work in ASP application? What i am expecting i need to develop a single component it should support for both ASP and ASP.NET application :) Is it possible to work in WCF REST Service? – subramani Dec 16 '11 at 09:29
  • Thaks for your response.In my WCF REST Service have method GetData(string url), the client app like ASP or ASP.NET it will invoke the method pass the URL like http:\\localhost\test\default.aspx ... based on the URL it will write into (.html) file. Its is working in Static Page, but dynamic content like click the button it will fetch the record from db and binded into grid at the situvation its not working how should i integrate the above code...... Please guide me on this – subramani Dec 16 '11 at 10:26
  • http://stackoverflow.com/questions/1020045/how-to-trace-scriptservice-webservice-requests – sikender Dec 16 '11 at 10:36
  • chack above link. I think it helps you. – sikender Dec 16 '11 at 10:36
  • Thanks its working fine for ASP.NET application, guide me on ASP application. – subramani Dec 16 '11 at 13:26