Here is the code that remove the whitespace in your rendered ASPX page from server.
Make Class1.cs file in App_Code folder and Put this code it it,
and Register this Module from web.Config file.
and Register this Module from web.Config file.
This reduce your pageSize to render and Increase Performance.
using System;
using System.Web;
using System.IO;
namespace SampleCSharpApplication
{
public class Whitespace :
Stream
{
private
Stream OutPut;
private
bool inWhiteSpaceRun = false;
private
bool whiteSpaceRunContainsLineBreak = false;
public
Whitespace(Stream output)
{
this.OutPut
= output;
}
public override void Write(byte[] buffer, int
offset, int count)
{
int
dataLimit = offset + count;
for
(int i = offset; i < dataLimit; i++)
{
char
c = (char)buffer[i];
if
(Char.IsWhiteSpace(c))
{
inWhiteSpaceRun = true;
if
((c == '\r') || (c == '\n'))
{
whiteSpaceRunContainsLineBreak = true;
}
}
if
(inWhiteSpaceRun)
{
if (whiteSpaceRunContainsLineBreak)
{
OutPut.WriteByte((byte)'\r');
OutPut.WriteByte((byte)'\n');
}
}
else
{
OutPut.WriteByte((byte) 0);
}
OutPut.WriteByte((byte)c);
inWhiteSpaceRun = false;
whiteSpaceRunContainsLineBreak = false;
}
}
public override bool CanRead
{
get
{ return true;
}
}
public override bool CanSeek
{
get
{ return true;
}
}
public override bool
CanWrite
{
get
{ return true;
}
}
public override void Flush()
{
OutPut.Flush();
}
public override long Length
{
get
{ return 0; }
}
public override long
Position
{
get
{
return
0;
}
set{
}
}
public override int Read(byte[] buffer, int
offset, int count)
{
return
OutPut.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin
origin)
{
return
OutPut.Seek(offset, origin);
public override void SetLength(long
value)
{
OutPut.SetLength(value);
}
}
public class Module : IHttpModule
{
private
void OnPostRequestHandlerExecute(Object sender, EventArgs
e)
{
HttpApplication
httpApplication = (HttpApplication)sender;
HttpResponse
httpResponse = httpApplication.Context.Response;
if
(httpResponse.ContentType == "text/html")
{
httpResponse.Filter = new Whitespace(httpResponse.Filter);
}
}
public void Init(HttpApplication
httpApplication)
{
httpApplication.PostRequestHandlerExecute += new
EventHandler(OnPostRequestHandlerExecute);
}
public void Dispose()
{ }
}
}
In Web.Config file.
<httpModules>
<add name="WhiteSpace" type="SampleCSharpApplication.Module" />
</httpModules>
</httpModules>
Ref. from Mads Kristensen's blog
No comments:
Post a Comment