web 2.0

Clearing Extra White Lines from Pages HttpModule

Hi,

Here is a pretty useful HttpModule in order to clear Extra White lines from ASP.NET Pages using an Response Filter.

Here is the HttpModule code

public class ClearWhiteLinesModule : IHttpModule
{
    #region IHttpModule Members

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(BeginRequest);
    }

    public void Dispose()
    {
    }

    #endregion

    private void BeginRequest(object sender, EventArgs e)
    {
        HttpApplication Application = sender as HttpApplication;
        String OriginalString = Application.Request.Url.OriginalString;
        if (OriginalString.ToUpperInvariant().Contains(".ASPX"))
        {
            Application.Response.Filter = new ClearWhiteLinesFilter(Application.Response.Filter);
        }
    }

    private class ClearWhiteLinesFilter : Stream
    {
        #region Private Fields

        private Stream _originalFilter;

        #endregion

        #region Constructors

        public ClearWhiteLinesFilter(Stream originalFilter)
        {
            _originalFilter = originalFilter;
        }

        #endregion

        #region Public Overrides Properites

        public override bool CanRead { get { return true; } }
        public override bool CanSeek { get { return true; } }
        public override bool CanWrite { get { return true; } }
        public override long Length { get { return 0; } }
        public override long Position { get; set; }

        #endregion

        #region Public Overrides Methods

        public override void Flush()
        {
            _originalFilter.Flush();
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            return _originalFilter.Read(buffer, offset, count);
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            return _originalFilter.Seek(offset, origin);
        }
        public override void SetLength(long value)
        {
            _originalFilter.SetLength(value);
        }
        public override void Close()
        {
            _originalFilter.Close();
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            Byte[] data = new Byte[count];
            Buffer.BlockCopy(buffer, offset, data, 0, count);
            String html = Encoding.Default.GetString(buffer);

            // Remove Extra White Lines
            html = html.Replace("\r\n\r\n", String.Empty);

            Byte[] outdata = Encoding.Default.GetBytes(html);
            _originalFilter.Write(outdata, 0, outdata.GetLength(0));
        }

        #endregion
    }
}

Here is the Web.Config section modification to implement this HttpModule.

<httpModules>
    <add type="ClearWhiteLinesModule" name="ClearWhiteLinesModule"/>
</httpModules>

 

 

Hope this help's!



Views(731)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

ASP.NET | C#

Comments

Technorati Profile