Today I am going to show you how I have implemented Condition Get for my website tariqulazam.info. Those who are not familiar with conditional get can have a look at the definition of conditional Get below.
From Http1.1 spcification : The semantics of the GET method change to a "conditional GET" if the request message includes an If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client.
To understand the scenario clearly, let’s have a look at the following two http response header of tariqulazam.info homepage, one for the initial request and the other is for when the user returns back to the home page later. I have used HttpFox to capture this headers.

Figure 1: Response headers for two request for same page when Conditional Get is not used.
If you look at the (status-line) and Content-Length header, you will see that status-line and the content length is same for both request. That indicates even if the content of the home page is not modified during the time between this two visits, every request to the home page cost us almost the same amount of data transfer. Conditional Get comes in to action in this scenario by reducing the data transfer for the subsequent requests to the same page.
So, if you understand what conditional get is, it is now time to have a closer look at the request header. When a client visits a webpage for the first time, in short when the client does not have cached entities for this webpage, the request header does not have any of these request header fields
If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match
When the server receives the request and it does not find any of those header fields, it just process the request and send the response to the client with a status code 200(OK). Next time when the client request the same page, it sends If-Modified-Since and If-None-Match request header fields and based on this values the server decides whether it should process the request or just send an 304 (Not-Modified) status code to the client. Note that the content length will be 0 if a 304 response code is sent. How the server will decide is dependent on the developer and it may vary depending on individual situations.

Figure 2: Request and Response headers for first request on tariqulazam.info homepage when conditional get is enabled.
.
Figure 3: Request and Response headers for subsequent request on tariqulazam.info homepage when conditional get is enabled.
If you have still difficulties understanding Conditional GET, read this article to have a more closer look at the headers and what they do.
Lets see some C# code now on how to implement this in our website. I have attached a complete website, so you can now download it and have a look.
Start by putting a Webhelper.cs class file in the APP_Code folder. It has got just 1 public static method and 2 private static methods. I want to keep the algorithm pretty simple to find out when the content of the website has changed. For tariqulazam.info website, when a resource link, article or comments is added or updated, I keep the modified time in a static field and used that value to generate E-Tag for the content being supplied by the IIS. Later when the same page is requested I just check the If-Modified-Since and If-None-Match request header value to determine whether I should process the request or just send response code 304 without processing the request. But it is totally upto you when you send the not modified response.
Note: If you find cached response for subsequent request try a hard refresh using F5 key to get the Not Modified Header. Also try to run this sample directly by creating application on your IIS. Casini (Visual Studio built-in webserver) does not handle some of these headers properly.
Please feel free to ask any question or leave any comment you may have about this article.