Filtering or reducing Whitespace from HTML or Dynamic HTML contents in page
<p>I am new to ASP.NET and I have Googled this question which I rather not do because being new to ASP.NET, simply copying and pasting from Google gives you zero knowledge.</p>

<p>I am working with a specific book that showed me how to do this, however, it is not working. There are no Errata on the books website and so I am thinking the issues may be related to my implementation.</p>

<p>I need to reduce <em>White Space</em> from my pages that are more than a line break. I do not want all the codes meshed together but at least a space between groups of code.</p>

<p>I created an HTTP Modul <strong><em>Module.cs</em></strong>:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace authentication2
{
public class Module: IHttpModule
{
public void Init(HttpApplication httpApplication)
{
    httpApplication.PostRequestHandlerExecute +=
        new EventHandler(OnPostRequestHandlerExecute);
}

private void OnPostRequestHandlerExecute(Object sender, EventArgs e)
{
    HttpApplication httpApplication = (HttpApplication)sender;
    HttpResponse httpResponse = httpApplication.Context.Response;

    if (httpResponse.ContentType == "text/html")
    {
        httpResponse.Filter = 
            new WhiteSpaceFilterStream(httpResponse.Filter);
    }
}

public void Dispose()
{
}
}
}
</code></pre>

<p>I than created a filter class: <strong><em>WhiteSpaceFilterStream.cs</em></strong></p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace authentication2
{
class WhiteSpaceFilterStream : Stream
{
    private Stream outputStream = null;

    public WhiteSpaceFilterStream(Stream outputStream)
    {
        this.outputStream = outputStream;
    }

    private bool inWhiteSpaceRun = false;
    private bool whiteSpaceRunContainsLineBreak = false;

    public override void Write(byte[] buffer, int offset, int count)
    {
        int dataLimit = offset + count;
        for (int i = offset; i &lt; dataLimit; i++)
        {
            char c = (char)buffer[i];

            if (Char.IsWhiteSpace(c))
            {
                inWhiteSpaceRun = true;

                if ((c == '\r') || (c == '\n'))
                {
                    whiteSpaceRunContainsLineBreak = true;
                }
            }
            else
            {
                // If this is the end of a white space run, write a single
                // white space or line break
                if (inWhiteSpaceRun)
                {
                    // If the white space run contains a line break,
                    // write a line break, so we don't break JavaScript.
                    // Otherwise just write a space.
                    if (whiteSpaceRunContainsLineBreak)
                    {
                        outputStream.WriteByte((byte)'\r');
                        outputStream.WriteByte((byte)'\n');
                    }
                    else
                    {
                        outputStream.WriteByte((byte)' ');
                    }
                }

                outputStream.WriteByte((byte)c);

                inWhiteSpaceRun = false;
                whiteSpaceRunContainsLineBreak = false;
            }
        }
    }

    public override bool CanWrite
    {
        get { return true; }
    }

    public override bool CanSeek
    {
        get { return false; }
    }

    public override bool CanRead
    {
        get { return false; }
    }

    public override long Length
    {
        get { throw new InvalidOperationException(); }
    }

    public override long Position
    {
        get { throw new InvalidOperationException(); }
        set { throw new InvalidOperationException(); }
    }

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

    public override int Read(byte[] buffer, int offset, int count)
    {
        throw new NotImplementedException();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        throw new NotImplementedException();
    }

    public override void SetLength(long value)
    {
        throw new NotImplementedException();
    }

    public override void Close()
    {
        outputStream.Flush();
        outputStream.Close();
        base.Close();
    }
}
}
</code></pre>

<p><em><strong>Finally I added this to the web config file:</em></strong></p>

<pre><code>&lt;system.web&gt;
&lt;httpModules&gt;
&lt;add name="WhiteSpaceFilter" type="authentication2.Module, WhiteSpaceFilter"/&gt;
&lt;/httpModules&gt;
&lt;/system.web&gt;
</code></pre>

<p>I have played with the config file but not sure if it is right: <strong>authentication2</strong> is the name of the project.</p>

<p>I do not get any errors, however, the white spaces are not reduced.</p>

<p>I see many examples but most refer to MVC, which I don't know much about.</p>

<p>I am stuck!</p>

<p>Thank for your time.</p>
