Manually Compressing Any ASP.NET Page, PageMethod, MVC Action, HTTPHandler,..

Compressing A Single ASP.NET Response Manually

This post is about compressing your HTTP result without using IIS Dynamic Compression. I’ll save the story to the end and give you the code first:

Well, this shows how to do the compression itself. Depending on how you do ASP.NET, you probably will call it differently.

In my case, I called it manually from an ASP.NET Webforms PageMethod (more on why below), but if you are using ASP.NET MVC for example, you probably want to wrap it in an ActionFilter and apply that to the action you want to compress its output. Let me know in the comments or on twitter if you have a problem implementing it in a particular situation.

IIS Dynamic Compression

IIS 7+ has built in dynamic compression support (compressing output of server-side scripts like ASP.NET, PHP, etc.). It’s not by default because compressing dynamic content means running the compression for every request (because it doesn’t know what the server-side script will generate for every request, the point of using server-side programming is generating dynamic content!).

Static compression on the other side (caching static files like styles and scripts) is on by default because once the static resource is compressed, the compressed version is cached and served for every future request of the same file (unless the file changes of course).

General advice: I’d say if your server side scripts expect to return large text-based content (say, large data, even after paging, etc. like large reports or whatever), always turn dynamic compression on, at least for the pages that expect to return large data sets of text.

In many cases though the majority of large files will be scripts (and possibly images) will be the larger parts though, which are often taken care of (for scripts for example) by IIS static compression or ASP.NET Bundling.

Creating Absolute Urls Of Controller Actions In ASP.NET MVC

An Example Of The Need To Use Absolute URLs

I have been doing some work around twitter and ASP.NET MVC. The way twitter authentication works, is that I have a page that creates a twitter URL, redirects the user to twitter, imagethe users accepts to use the application associated with the website, and twitter redirects the user to a callback URL, the completes processing of the user credentials. In order to set a callback URL dynamically (especially in development, when the callback is likely a localhost one), we need to send the absolute URL to twitter.

Other examples might include having a “permanent URL” for some resource (product, blog post, etc..), or maybe a link to be used in emails or so. There can be many usages, so, let’s see how to do it!

How We Did It In Webforms

In webforms, the easiest way to do it was to use the Control.ResolveClientUrl() method.

Typically you pass a URL relative to the current code file (.aspx, .master or .ascx file). and it returns the corresponding absolute URL of that. Of course when the file is a control or a master page file, we don’t always want/have a path relative to this file. The work around for this was passing a relative URL that starts with “~/”. As you probably know, “~/” represents the root of the website.


Assuming your website is running in “https://localhost:4444/my-application”, calling:

from any page or control wiill return “https://localhost:4444/my-application/About/Contact-Us.aspx”.

Fully Qualified Urls In ASP.NET MVC

Similar to webforms, where we used a current control as a starting point, we can use the controller (or view, if we really want to [hint: we don’t]), to access the current instance of “UrlHelper” class (the “Url” property of an ASP.NET MVC Controller), which gives us access to the routing system that comes with ASP.NET in general, and gives shortcut methods specific to ASP.NET MVC, like Url.Action().

This will return us the relative URL though, to convert this to an absolute / fully-qualified URL, we use Request.Url.AbsoluteUri (Controller.Request is the current HttpRequestBase instance) to get the absolute Uri information, and the “UriBuilder” class to create the Url.


Turns out you can call any of the UrlHelper methods and get an absolute URL directly if you call the overload that accepts a “protocol” value (also called “scheme”, that’s  “http”, “https”, etc..), even if the protocol is the same protocol used in the current request.

Original Example

Going with the same assumptions in the webforms example, replacing “contact-us.aspx” page with a controller “AboutController”, and an action “ContactUs” that has ActionName set to “Contact-Us”, adding the following code inside any ASP.NET MVC action:

can be used to get “https://localhost:4444/my-application/About/Contact-Us”.

Updated Example

We can get “contactUsUriString” in the previous example in a different way, by calling:

Or alternatively even more compact:

Of course we could change the action name and routing etc to maintain lower case, or do it from IIS or so, but doing this would be too much to the point this blog post is concerned about

Hope this was useful to you.


Ajax Control Toolkit New Release, HTML Editor, Combo Box, Color Picker

the AjaxControlToolkit has a new release, the release includes bug fixes and new HTMLEditor, ComboBox, ColorPicker controls/extenders.

HTML Editor

I liked the HTMLEditor control so much from first sight. Pretty lightweight and customizable. I still need to check whether it works fine inside UpdatePanel and whether it can be used with RequiredFieldValidator or so.

image  image


Color Picker

The ColorPicjker is just an extender to TextBox control. This means a lot of opportunities and promises less problems with validators, etc… It’s a very nice one too.



Combo Box

The ComboBox is a control that is like a DropDownList with auto complete except that the user can enter a new text that is NOT in the existing items. This is good or bad based on your sole use.

I did not like how it’s not well styled in the example page. This means it’s going to be headache to style in my sites as well, which is one of the biggest problems with AJAXToolkit in general.




Prevent ASP.NET Validators from Massively Increasing Page Size

This is problematic with ASP.NET AJAX. The main Script Components are NOT sent to the client when in :Legacy” mode. This is “By design” in ASP.NET AJAX, although it is a clear limitation!!

I’m investigating the problem for other solutions and will be sending an update soon.

Thank you, Iman Halawany, for making me note this. I’ve been working on WCF services and ASP.NET MVC stuff lately than normal webforms, so, didn’t realize this obvious showstopper.

To all my readers, I owe you a BIG apology.

The Problem

ASP.NET validators and ValidationSummary controls are rendered as SPAN tags that are shown and hidden based on validation state. The properties of the validators are written normally via JavaScript calls similar to these:

<script type=“text/javascript”>


var Page_ValidationSummaries =  new Array(document.getElementById(“vdsSiteLogin”));

var Page_Validators =  new Array(document.getElementById(“rfvEmail”), document.getElementById(“revEmail”), document.getElementById(“rfvName”));




<script type=“text/javascript”>


var rfvEmail = document.all ? document.all[“rfvEmail”] : document.getElementById(“rfvEmail”);

rfvEmail.controltovalidate = “txtEmail”;

rfvEmail.errormessage = “Email Missing”;

rfvEmail.validationGroup = “SiteLogin”;

rfvEmail.evaluationfunction = “RequiredFieldValidatorEvaluateIsValid”;

rfvEmail.initialvalue = “”;

var revEmail = document.all ? document.all[“revEmail”] : document.getElementById(“revEmail”);

revEmail.controltovalidate = “txtEmail”;

revEmail.errormessage = “Email is invalid”;

revEmail.validationGroup = “SiteLogin”;

revEmail.evaluationfunction = “RegularExpressionValidatorEvaluateIsValid”;

revEmail.validationexpression = “\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”;

var rfvName = document.all ? document.all[“rfvName”] : document.getElementById(“rfvName”);

rfvName.controltovalidate = “txtEmail”;

rfvName.errormessage = “Password is missing”;

rfvName.validationGroup = “SiteLogin”;

rfvName.evaluationfunction = “RequiredFieldValidatorEvaluateIsValid”;

rfvName.initialvalue = “”;

var vdsSiteLogin = document.all ? document.all[“vdsSiteLogin”] : document.getElementById(“vdsSiteLogin”);

vdsSiteLogin.validationGroup = “SiteLogin”;





Note that this is for ONLY 3 validators, 1 validation summary and in directly a page that doesn’t use a master page, not in nested user control or such!

How about a page with over 30+ validators (yeah, those forms!!), and each with ClientID like “ctl00_cphBody_ct00_fvUserLogin_rfvEmail_” and such?

If you have ever wondered why those pages take so much time loading, this code block (multiplied per number of validators you have and their properties set) is one reason.

You cannot even take the JavaScipt in separate file that can be cached, because this is dynamically created as per the visible validation controls.

The Solution (See Above Note)

The clear alternative to setting those properties via JavaScript long block with huge ClientIDs is to put the properties in the SPAN tags of the validation controls themselves.

The reason that AS.NET does not do this by default is that this is not XHTML 1.0 Transitional compliant, because the validator properties are not XHTML attributes of the SPAN tag.

ASP.NET tries to render XHTML 1.0 Transitional markup by default. But you can change that in your web.config file by adding one line under <system.web>:


        <xhtmlConformance mode=Legacy/>


This will make the properties render in the SPAN tags themselves, saving so much code in real life scenarios.

Personally I’d recommend: DO THIS IN EVERY WEBSITE YOU HAVE (See above note)


Thanks to Paulo Morgado for mentioning this.

ASP.NET Validators Client Side APIs: MSDN List Page

If you have ever thought that the famous if(Page_ClientValidate("validationGroup")) {/*JS Code*/}  and myValidator.ValidationGroup = "validationGroup"; are sure not enough client side capabilities in ASP.NET validators, you are right.

The list of client side API for ASP.NET Validators can be found on this MSDN page "ASP.NET Validation in Depth":

Look for subtitle: "Client-Side APIs".


Thanks Simone Chiaretta for mentioning the topic, Mohamed Tayseer for sharing the topic on facebook, and Richard Cook for his comment on the post making me search for the complete list.

ASP.NET 3.5 Extensions, MVC, Expression Studio and Silverlight & IE 8 Preview Updates

With MIX, and meeting the announced MVC roadmap, the new versions of AS.NET Extensions are now publicly available.ASP.NET MVC Framework Preview 2 is available also as separate download, same as Silverlight 2.0 BETA 1 (and Expression studio, Silverlight VS 2008 Tools).

Download Links:

Additional Resources:

  • Announcements

    Have fun :)

    Unit Test Friendly File Upload In ASP.NET

    The Problem

    In an N-tier application, you keep your logic in a business logic tier, typically a different VS project that can be used from a website, a windows service, or desktop application, and that should be valid to writ unit tests against on its own.

    But how about if your requirements say that you need to to upload some file for the business logic to work? Think of a scanned image (signed contract maybe?) or just a comma separated value file containing some emails.  Typically the business logic tier will be the place to to handle this, but how can you send the uploaded file to it? You can get the file as “HttpPostedFile” from the “Request.Files” collection or the file upload control itself, but, to receive it in the business logic, project, the easiest way is to add reference to “System.Web” dll and accept the type “HttpPostedFile” as method argument or so, and when in need to save the file physically, you call the “SaveAs” method of “HttpPostedFile” … So simple right ?

    But how about unit testing? “HttpPostedFile” has no public constructor and is sealed class (you cannot inherit from). How will you write a unit test for a method that accepts “HttpPostedFile” as argument to work? You’d go finding a way to mock that or just forget about testing that particular method!!

    A Simple Solution

    Well, it’s more simple than you think. An “HttpPostedFile” has a property called “InputStream”, which is of type “System.IO.Stream”. Hey, that has nothing to do with “system.Web” :). You can create your own FileStream or whatever other stream in the unit test, and then only worry about other areas you want to test related to the method in your business logic.

    Because I know dealing with streams is quite ugly (at least it is to me!), I wrote an example that will walk you though a sample usage of that property. I have the complete example VS solution as attached file at the end of the example code below.

    The Business Logic

    In a real world example, the business class will have various parameters including the file, but in this example, I have made my business class (I call it “FileLogic”) just worry about the file. It has a method that treats the stream coming to it as containing just text, reads that text and returns it. Note the type of the only method parameter

    Another example of a method in the business logic project is one that actually saves the file. Instead of just calling “SaveAs(filepath)”, you need to copy the stream yourself. Usually there’s more than one way to do so, the problem in most of the ways is determining the size of the stream. An “HttpPostedFile” knows the size of the file, but as I’m not getting the “HttpPostedFile” (and it wouldn’t sound great if I requested the file size as a method parameter!), I dealt as if I do not know the actual file size. See the code for this method:

    Example: A Webforms ASP.NET Project

    Before we dig into the unit test sample, let’s see how our page code will look when dealing with this business logic class.

    Consider a page with controls like these:

    The code behind for such page with our custom logic class (in a sample manner still), would look like this:

    I’d say that this part is not much different than it’d be if we were passing the “HttpPostedFile” object complete.

    The Unit Test

    So, that’s what we have been hassling, complicating code, and dealing directly with streams for its sake!

    As our application will not necessarily be a file sharing application, we’ll be in need for the file as part of a bigger operation. A unit test will not be only concerned with the file upload task, but also the rest parts of the business operation our method intends to do. However, in this sample, I’ve decided to make it just test the file uploading functionality, and included really simple ways in the unit test.

    Instead of the file upload, I used a physical file that I get its path from a method called “GetTestFilename” (I could have used configuration file, static property or else for the file stream, or used any other kind of stream still).

    See how the test methods look in this example (using NUnit):

    Link List: jQuery, WF, WCF, OOP and Patterns, ASP.NET, Scrum,…

    This is also from an internal mail to SilverKey Tech Egypt office dev. team. Slightly modified this time! (Yes, I know. I should stop this habit of link collection and get back to writing – hopefully soon)

    hmm, how about more links as a blog readers bonus,..

    GridView DataBinding Events

    This is a well commented example for a GridView with implementation of RowDataBound and DataBound events. I also demonstrate in it some of the important properties of GridView, like the slight differences when dealing with Grid Paging, Rows, Cells, and Columns. I prefered to heavily comment the code than to write separate paragraphs describing it as I do believe that the code is what gets the idea in a direct way.

    The example is a simple page that uses SqlDataSource to connect to a SQL Server 2005 Express database and show the results on a GridView with paging capability.


    Many parts of the code don’t show the best practices for their situation and those are only included for demonstration purpose as they are not the main focus of the example.

    Let’s start with the code behind of the page:

    As it’s the most important part

    Now to the page XHTML source:

    Samples of the output of the code:

    Hope that you like it.