Asp.net web application performance is one of the key factors in online business success.
In this post I will try to cover all different asp.net frameworks, including asp.net core website performance improvement tips; so all points may or may not be relevant to your asp.net application depending which framework you are using at your project.
Here are 10 simple tips to increase Asp.net Application Performance drastically.
Now we have few different framework of Asp.net, most of earlier application were written in AspX with code behind, later we started with Asp.net MVC, then now we have Asp.net Core, so when we talk about improving performance, every framework has some different way of processing request, so keeping that in mind in this article I will cover AspX code behind and Asp.Net MVC, about asp.net core I will publish separately.
Here are some ideas on Asp.net website performance improvement.
In earlier asp.net framework Viewstate was one of the main reason to slowdown page load and every time the page post occur,
but that was the mechanism to hold data between page post back,
so if your some page does not have any form or posting data is not necessary,
please turn off Viewstate for that page, just by setting EnableViewState = "false"
in your page directive, that will not affect any other page of that application, but improve the page loading time significantly.
Try, not to write any in-line CSS or any style block on page, put them all in one css file and add the reference, same for JavaScript code, avoid writing any JavaScript code on page, put all JavaScript in separate file and use the reference , this will help reducing the page size to some extent.
In page html, don’t use any table tag, use all div, span etc, try to use bootstrap css, you get many ready-to-use css classes, which are responsive by default , this will help page rendering faster.
Find out all common data which are being loaded from database when every time page gets loaded, common data means those maser data which remain same, or not going to change very frequently, data like country list, product master etc. you can store them in server side cache object, so instead of pulling them from database, you can fetch them from cache, that will help reducing some time by avoiding unnecessary trip to database server.
Server side caching can improve application performance significantly.
You must make sure all images are well optimised for webpage, make images as lighter as possible, so that can be loaded faster, probably you need to take help from some Photoshop expert, there are some tool to make good quality and lighter image for web.
If you use HTTP compression rightly, page size can be reduced to at least 50 percent, it removed all additional spaces in your rendered html, though there is not ready-to-use tool to compress content , but you can write a filter using ActionFiltterAttribute , here is an example of how to write http compression filter in ASP.NET
You can use Page Output Caching, that will help page loading faster, can improve performance drastically, you can set same page caching by parameter also, means depending on parameter different version of same page will be cached in browser, so when user visit the same page, the page will be loaded from cached. Here is an example of how you can implement output caching in asp.net.
if your whole page is displaying dynamic content, but some part of your page has static content, you can use partial caching (known as fragment caching), which means only some portion of page will be cached, not the entire page.
Asynchronous Methods will not block UI of the Page, also will not wait till one task is completely done, and it will keep loading content asynchronously, thus content loading become much faster, and give end user a good experience of browsing your page.
Learn how to make Async page in asp.net mvc
Try to use await
keyword, instead of Task.Wait
or Task.Result
.
When you use wait, you are actually blocking thread until the task is completed, which works synchronously, but when you use await keyword, it does not block the thread, works asynchronously.
// Good Example Task task = GetStudents(); await task; // Bad Example Task task = GetStudents(); task.Wait();
How you fetching data from database that may make huge difference, sometimes even after doing all implementation correctly, application still perform very slow just because of poor data access implementation, though most of Asp.net application uses ORM, LINQ , but here are few simple things may make huge difference.
When you are thinking of data access performance optimization, Ado.net can give better performance than Entity Framework.
Avoid using application variable if not necessary, even if using then never store any large data or master data in application variable.
Use session variable only to keep user specific data for that particular duration, and nullify the session object as soon as user leave the site, using too many session variable for multiple users may slow down application performance, so avoid as much possible.
You can minify all your JavaScript and css files using bundling techniques, in BundleConfig file you can add those files in different bundle like example below.
bundles.Add(new StyleBundle("~/Content/css") .Include("~/Content/etg_style.css", "~/Content/wtr.css", "~/Content/bootstrap.min.css", "~/Content/bootstrap.css"));
Use lazy loading whenever you want to loading collection object, lazy loading is a technique that can really help improving application performance by reducing exaction time, c# lazy class allow us to load data on demand, instead of when the instance is created.
Lazy<List<int>> list3Lazy = data.GetList3(); public class data { public static Lazy<List<int>> GetList3() { Lazy<List<int>> list3 = new Lazy<List<int>>(); for (int i = 0; i <= 50; i++) { list3.Value.Add(i); } return list3; } }
Learn how to implement lazy loading in C# application development.
Optimizing your database objects is equally important, You may also read how to improve SQL Database Performance
You may be interested to read following posts: