Optimizing ASP.Net Application – The Presentation Layer (Part 1)
So how do we go about optimizing the presentation layer ? I am not gonna talk about View State or Session as it has been discussed all over the web time and again and by now people you would have already implement view state selectively and should be handling sessions with care and also implement GZIP compression over the session (Visit ASP.Net 4.0 enhancements if you are not doing this already).
When you request for any web page, there are two things which happens:
- The HTML (including data) is downloaded from the server
Most of the times when you talk about optimizing your ASP.Net sites, you would look into things like how fast you could get the data from the database and how you could implement things like App Fabric Caching, etc.
What you miss out is the fact that almost 75 % of your page load time would be attributed to downloading and rendering the so called resources and not the actual HTML + data. At least that what the statistics says.
So what can you do about this ? First you need to understand what to fix. If you are using IE 9 as the browser, you can use the developer tools to understand how does your page statistics look like. I created a simple application which has an image control and some image thumbnails. You click on a thumbnail and it loads that image on the image control. All using a very simple JQuery. I ran the developer tools to see the network profile and I was shocked to see the results:
Alright I have a clear problem with my website and the high load time is because of my resources. Now what ?
Well go fix them. To help with this better, you could use Yahoo YSlow or Google Page Speed. Unfortunately you don’t have a version of these that will work with IE, so you would have to use Firefox or Chrome even though you know IE is the best browser .
Once you run this tool against your page, it would tell you what the issue is. Here are the major ones which will impact your performance greatly:
One of the most common error I’ve seen in my career is that people conveniently include all their script references in the Head section. Well this is not a great practice. Always try and put then after the page’s UI content. Only CSS references should be provided in Head and not Script. If at all you don’t want to cut all those lines and put them down, use the defer property.
The defer attribute specifies that the execution of a script should be deferred (delayed) until after the page has been loaded.
Add Expires Header
Always try and cache the local resources in the browser so you would not have to download them every single time. This can very easily be done for your ASP.Net application.
Jut go to web.config and add the following to <system.webServer>
<staticContent> <clientCache httpExpires ="Fri, 29 Mar 2020 00:00:00 GMT"/> </staticContent>
What happens is that all static content will now have an expires HTTP header set to the year 2020. Static content means anything that isn’t served through the ASP.NET engine such as images, script files and styles sheets.
Minimize the number of requests
You could write custom HttpHandlers to do this or you could use something already available like SquishIt
<%= SquishIt.Framework.Bundle.Css() .Add("~/Styles/Site.css") .Add("~/Styles/styles.css") .Render("~/Styles/combined_#.css") %>
Another technique to reduce the load time on the client is to make sure that all your JS and CSS resources are minified. This allows you to reduce the size of your resources. One of the minifiers I have come across is the Microsoft Ajax Minifier. It’s a command line utility which also provides API so you can minify your resources at the time of deployment or even dynamically by using the API.
There are few other techniques which I will talk about in the next part of this post.
Happy Coding !!