Some of you find it useful

Listing of my everyday findings about .NET

  • nature
  • nature
  • nature
  • nature
  • nature
  • nature

ASP.NET - Posts(3)

Link to some very useful eBooks

by tariqulazam

Lately my manager here at The Center for Learning Innovation shared some eBook links and i have already finished reading some of them. Most of them regarding best software development practices and application performance. I found them very useful and thought you would also like them. I would highly recommend this books for developers who has just started object oriented design and want to adopt best practices in this field. No matter in which platform you work, it will definitely enrich your arsenal to fight against different development challenges. 

You are probably aware of the following eBooks from Microsoft, they are pretty big (to be honest, i have read only some chapters) but worth reading if you are working in .NET 

Thanks to Refky, for sharing those with me and I hope you will like them.

 

Post category: ASP.NET, C#, Design Pattern, Others

Donut Caching with ASP.NET Substitution control

by tariqulazam

We all are using ASP.NET Caching features to improve the performance of our site and we all know about very widely used caching terms like 'Output caching','Fragment Caching' etc. For those who are not familiar Google is your friend.

Today I am not going to show how caching can be used rather I will describe a hidden gem in the cache infrastructure of ASP.NET. I am calling it hidden and Gem at the same time as I did not encounter any scenario before where this could be used (or I did not even think about caching…) and where applicable it could be very handy. It’s called ‘Donut caching’ and as the name implies, it is like a hole in the cached output where you can programmatically set value.

Let me tell you the scenario that I encounter today at work. We have a fairly large application and we are in the process of optimizing the performance of that application. It has got a Header User Control and a couple of controls in the header are populated based on logged in user roles. The logged in user name also displayed in the header. For some database operation heavy page (search result page), we want to implement output caching, but the barrier was the header control. We do not want to redesign the page and encapsulate the search result grid into another User control and implement fragment caching there. Here comes the ASP.NET Substitution control in our rescue.

To start with, let us look at the mark-up of the Substitution control first.

<asp:substitution methodname="GetUserName" runat="Server" id="subUserName"></asp:substitution>

The html rendered by this control is the hole in the donut. The only important and useful bit is the MethodName property. It should reference a static method and should return a string. So whatever string you return from the GetUserName method will be rendered in the output. For displaying the logged name user name we have used the following method. For populating other controls we create the markup as necessary in the similar fashion.

 Public static string GetUserName(HttpContext context){  	       
         return Session["UserDisplayName"].ToString();   
 }  

So far, everything was easy, but I am pretty sure you will run into issue soon, as most of the developer depends on Session for keeping some user specific information, like Display Name, etc. The above mentioned code will work fine on the first instance but you will encounter a NullReference exception when you come back to the page later. It is because, for the cache version of the page, the page was never created and hence no session. Despite this limitation, asp:Substitution is a very handy control, that could save you some of your time and headache.

I have attached a very simple website project here. Download it and see the asp:substitution control in action. I hope that helps and give you an idea when and how to use the substitution control.

Post category: ASP.NET, C#

Implementing Conditional GET in ASP.NET Website

by tariqulazam

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. 

Response Header Response header

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. 

Request Header Response header

Figure 2: Request and Response headers for first request on tariqulazam.info homepage when conditional get is enabled.

Request Header Response header.

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.

Post category: ASP.NET, C#, Others

Subscribe to RSS

Recent Posts

Recently Added Links

Article Archive

Article Categories

Article Tag Cloud

application performance asp.net c# caching cdn conditional get css design pattern donut caching etag html5 javascript n-tier application oop tips and tricks visual studio website performance

Links Categories

Link Tag Cloud

.net ado.net asp.net c# deployment design pattern entity data model features iphone jquery monotouch oop performance ria service silverlight tutorial video wcf website performance website project

Visitor Map