Hi,
Today a quick new HttpModule in order to moves the ViewState Hidden Input to the Page Bottom. This should be better for SEO because Search Engine Robots can read your page contents without parsing (firstly) a big no-sense block of text ...
Here is the HttpModule
public class BottomViewStateModule : 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 ViewStateFilter(Application.Response.Filter);
}
}
private class ViewStateFilter : Stream
{
#region Private Fields
private Stream _originalFilter;
private String _viewStateInput = String.Empty;
#endregion
#region Constructors
public ViewStateFilter(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);
html = MoveViewState(html);
Byte[] outdata = Encoding.Default.GetBytes(html);
_originalFilter.Write(outdata, 0, outdata.GetLength(0));
}
#endregion
#region Private Methods
private String MoveViewState(String html)
{
String viewStateLine = "<input type=\"hidden\" name=\"__VIEWSTATE\"";
Int32 startPoint = 0;
Int32 endPoint = 0;
if (String.IsNullOrEmpty(_viewStateInput))
{
startPoint = html.IndexOf(viewStateLine);
if (startPoint >= 0)
{
endPoint = html.IndexOf("/>", startPoint) + 2;
Int32 viewstateLength = endPoint - startPoint;
_viewStateInput = html.Substring(startPoint, viewstateLength);
html = html.Remove(startPoint, viewstateLength);
}
}
int formEndStart = html.IndexOf("</form>") - 1;
if (formEndStart >= 0)
{
html = html.Insert(formEndStart, _viewStateInput);
}
return html;
}
#endregion
}
}
Here is the Web.Config section modification to implement this HttpModule.
<httpModules>
<add type="BottomViewStateModule" name="BottomViewStateModule"/>
</httpModules>
Hope this help's!
Views(1547)

