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
}
}
ClearWhiteLinesModule.cs
More
No comments:
Post a Comment