About the author

Miron Abramson
Me
Software Engineer,
CTO at PixeliT
and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

Remove white spaces using HttpModule

Few weeks ago, Mads Kristens poseted a very usefull post about removing white spaces from your pages. For me, he found the best Regular Expression for this job.  He was using the Render method to do it, and it means that if you want to use it in existing site, you need to edit your base page and compile your site again.

I wanted to use it in some sites I have/manage, so I just insert his code into a new ResponseFilter and assign this filter in my Compression component, compile again this dll and replace it in all my sites. Importent thing is, that using this 'remove spaces metho' must folows the rule of that the current request is not made by MS-AJAX component/control, because the ViewState will be corrupted. This check can be easly made by this simple method:

internal static bool IsAjaxPostBackRequest(HttpContext context)
{
      return context.Request.Headers["X-MicrosoftAjax"] != null;
 }

Also note, that RequestFilters are excuted in order LIFO (the last added filter is the first one to be performed) , so if you use any compression filter (like me), make sure you register the compression filter before this one.

The ResponseFilter code can be download from here:

RemoveWhiteSpaces.cs (2.34 kb)

The compression module with this filter included can be download from this post: Compression component.  In this component you can control in wich pages you want to anable or disable the remove spaces option. (exclude specified pages from compression and remove spaces)

Currently rated 4.0 by 1 people

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

Categories: ASP.NET | C# | Server side
Posted by Miron on Thursday, December 27, 2007 1:00 PM
Permalink | Comments (3) | Post RSSRSS comment feed

Optimize your page for UpdatePanel - part II

In the previews post about Optimize your page for UpdatePanel I talked about removing white spaces from the ASPX to optimize the Update Panel callbacks because it can't be compressed as normal response, and thats due the special  format that this response is (it needs to be parse be JS in the client side).

The Update Panel response contain the new html for the specific location that needs to be update and the complete new ViewState for the whole page. So, one more thing we can to optimize the UpdatePanel is to compress the ViewState before it been sent to the client. We don't need to do it in normal response because we use (or should use) a compression module that compress all the page response and that includes the ViewState. Compress the ViewState can save you some more KB from the async response. Another option is to save the ViewState in the server (file os session), and not send it at all. To compress the ViewState we needs to override the 'LoadPageStateFromPersistenceMedium' and the 'SavePageStateToPersistenceMedium' methods that load and save the view state.

Here is the code how to compress the ViewState for the UpdatePanel response only, just copy it to your base page:

 protected override object LoadPageStateFromPersistenceMedium()
{
    string viewState = Request.Form["__COMPRESSEDVS"];
    if (viewState != null)
    {
         byte[] data = Convert.FromBase64String(viewState);
         data = Utils.Decompress(data);
         LosFormatter lf = new LosFormatter();
         return lf.Deserialize(Convert.ToBase64String(data));
    }
    else
    {
         return base.LoadPageStateFromPersistenceMedium();
    }
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
    if (Utils.IsMsAjaxCallback(Request))
    {
         LosFormatter lf = new LosFormatter();
         using (StringWriter writer = new StringWriter())
         {
             lf.Serialize(writer, viewState);
             string viewStateString = writer.ToString();
             byte[] data = Convert.FromBase64String(viewStateString);
             data = Utils.Compress(data);
             ScriptManager.RegisterHiddenField(this, "__COMPRESSEDVS", Convert.ToBase64String(data));
         }
    }
    else
    {
         base.SavePageStateToPersistenceMedium(viewState);
    }
}

 (The class 'Utils' can be download here: Utils.cs (1.15 kb))

Currently rated 4.5 by 2 people

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

Posted by Miron on Saturday, November 24, 2007 3:16 AM
Permalink | Comments (14) | Post RSSRSS comment feed

Optimize your page for UpdatePanel

One of the most (or the most) common use MS-AJAX control is the UpdatePanel.  It let us send data to the server and get response asyc, and change part of the page without refreshing the page, and it give us faster and more nice user interaction. Altho it sound perfect, it have big limitation.  While compressing the page, remove white spaces in the HTML and make our page very light, it is impossible  to modify the response for the update panel. In every method (in the Render event or any Handler/Module) that we made that modify the response, we must exclude the respose that made by the UpdatePanel.  The result is that we have a page with UpdatePanel that refreh a part of the page, we compressed the page and made it very light (lets say we compressed it from 30Kb to 5Kb). When we click the button that activate the update panel, it send the update panel request and receive its response, but this response is not compressed and can be sometimes even bigger than the complete page (that was compressed). It's a big contradiction!

There is a small thing we can do, it's really small, but better than nothing. The response for the UpdatePanel is build base on the original ASPX (before any compression), so, what we can do is, to optimize our original ASPX by removing any whitespaces. That will make the UpdatePanel response a little smaller.

What I use to do, is publish the site, and before uploading the files to the server, I optimize the ASPX files that have any UpdatePanel.

It is realy not big, but it is something.

In the bottom, you can download a tool I made to remove the white spaces from the ASPX and optimize it. Just copy your ASPX code and click optimize, and save that optimized code. 

OptimizeASPX.exe (20.00 kb)

Currently rated 5.0 by 2 people

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

Categories: ASP.NET | C# | Server side
Posted by Miron on Sunday, November 18, 2007 2:27 PM
Permalink | Comments (9) | Post RSSRSS comment feed