How to Improve ASP.NET Website Performance

In online business ASP.NET website performance or web application performance matters a lot. If your ASP.NET website is not optimized so the audience will press the back button and open an alternative. If you have an ASP.NET Application and it’s not optimized. The audience will sooner or later start complaining or find an alternative. Whenever we talk about user-friendly applications the optimization and performance are some of the key factors that we count first.

When we develop an ASP.NET Application we should take care of what elements we are using and how can we optimize each component better. If we just built an ASP.NET web application without considering the performance factor. So the application will be slow and you have to optimize it later or rebuild that application in a more optimized manner.

Today we are going to how we can build an optimized ASP.NET Web Application or how can we optimize an ASP.NET web application. There are many ways to improve ASP.NET Website performance but we will discuss some of the most efficient ways.

ViewState:

ViewState property provides objects to retain values between multiple requests for the same page in ASP.NET Application and ViewState data is serialized every time when ViewState is processed. So this processing increases the page loading time. ViewState is an extra feature for the end-user and it loads every time from the server. So when we use multiple controls like user registration it affects the performance more. ViewState can be disabled when it’s not needed. This can be disabled from “web.config” file in the ASP.NET Application.

<pages enableViewState=”false” />

It can be disabled for single control by following.

<asp:label EnableViewState="false" runat= "server" />

Caching:

Caching can improve any ASP.NET Application in a magical way. Caching is a mechanism in which the frequently used or static pages/data are stored into memory and can be served from cache. Client/server processes can be avoided by this technique.  Performance of ASP.NET Application can be improved by following ways.

Output Cache: With Output cache, the whole aspx page can be stored in the web server’s memory and be loaded just a few seconds from the cache without the client-server process. This technique is specially used if we have whole page static. This type of cache can be made valid or invalid and we can also change its duration by following.

<%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="browser" %>

Cache Object: With a cache object, any object can be stored in the web server’s memory and can be loaded in subsequent requests. Cache Object is specifically used to store serializable objects because these objects takes more time to load and increase processing time. Insert and remove methods can also be performed on these objects. Cache Object can be made invalid in some dependencies.

Cache.Insert("uservalues", userFileData, new 
    System.Web.Caching.CacheDependency(Server.MapPath("~/users.xml")));

Set CacheControlMaxAge:

As you can understand by its name Set CacheControlMaxAge, we can cache content for the duration we want. We can Cache static content with CacheConrolMaxAge for the custom duration we want. Gues what are the static files in any aspx page? CSS, Javascript files. The recommended duration store cache for one year. We can configure these settings in “web.config” file in the ASP.NET Application by following.

 <system.webServer>
  <staticcontent>
      <clientCache cacheControlMode="UseExpires" 
            httpExpires="Tue, 12 Dec 2022 03:14:07 GMT"/>
  </staticcontent>
  </system.webServer>

Try to Avoid Session:

The session is used to pass/transfer values or data between pages and it is valid for all requests not for only one. It’s the property of HttpSessionStateBase and we can store objects and variables in session. When the session variable is used/accessed multiple times on the page, then it will occupy more memory allocation. This is the big performance drawback of using sessions and creating overhead to IIS.

Try to avoid using sessions unnecessarily in your ASP.NET Application. To achieve better performance use, encrypted query string and cross-post back.

MiniProfiler:

Install MiniProfiler from Nuget Package Manager and configure it to ASP.NET Application. It is a great package that is developed to analyze the performance of the ASP.NET Applications. You can check your application response time and performance and can optimize it accordingly. Don not give access to the end-user and remove it from the Application after testing.

Use HTTP Compression:

As you can Understand by its name HTTP Compression is an algorithm that compresses HTTP files by removing extra spaces and redundancy from them. After compression of the file, the file size reduces and data can be transferred in a more optimized manner by taking less time.

Today All new modern browsers support HTTP Compression. Do not forget to turn on HTTP Compression. You can also use custom HTTP compression in your ASP.NET Application.

We can configure HttpCompression under system.webServer in web.config file of ASP.NET Application. It can be also enabled from IIS. Physical files can also be compressed by GZipStream, you can configure it before deploying production.

Use of CSS and Scripts:

Try to not use third-party scripts and CSS. If you are using third-party scripts make sure to download its source locally and configure it into your ASP.NET Application. If a single page is loading from multiple CSS and scripts, try to make a single so ASP.NET Application can load it in a single process. The application doesn’t have to process multiple times different files. This will reduce processing time and optimize application performance.

Try to use Async Scripts to load scripts Asynchronously.

<script async src="example.js"></script>

Do not use Long keywords as script files name, it takes longer than normal keywords.

Simple logic for Scripts and CSS is reducing the amount of data sent across the network can increase ASP.NET Application performance.

Image Optimization:

Using a lot of images and large size of images can reduce page loading speed because images take longer time to load. Do not use background images because this can be done simply by CSS. Always try to use small or compressed size od images and do not use a lot of images.

  • Use a reasonable width and height.
  • Compress image size before using.

JavaScript Validation:

With JavaScript, forms/data can be validated on the client-side. When form/data can be validated on the client side why validate on the server-side. On server-side validation, all data is sent to the server and after validation, the server sends back the response to the client-side.  It increases the process and time. Always use JavaScript Validation it saves a lot of process time.

Asynchronous AJAX:

Try to use Ajax asynchronously to retrieve data that is not required immediately like collapsed components, or data in another tab, and so on.

Garbage Collection:

.NET framework provides by default garbage collection to remove unused objects from memory. But it takes a long time to collect garbage.

There are many ways to remove unused resources but all methods are not recommended. Our concern is performance now so we use Dispose method for garbage collection in final block to improve ASP.NET Application performance. When using Dispose method we need to disconnect the connection immediately because it will free the memory and provide space into memory.

Remove Unused HTTP Modules:

Remove HTTP Modules that are not used in ASP.NET Application like windows authentication, SMTP, FTP and so on. By removeing unused HTTP Modules it reduces the load on server and improve response and request time.

Content Delivery Network (CDN):

Content Delivey Network is used to host images and script files. CDN reduces server load by caching these files.

Content Delivery Network is bunch of  web servers located in different locations. So we host our content to content delivery network and can send response to users more quickly from there nearest located server by analyzing there location.  CDN improves page loading time in a very efficient manner. CDN is used to improve ASP.NET Application performance.

Use of Pagination:

When a page have to load tons of records from database so the data take too long to load from server and page loading time increases. So when we have huge records to load and we use paging provided by .NET like ListView, GridView, and DataGrid take a lot of time load records from database. Pagination is used when we have huge data records and we load only necessary records that we have to show.  So we load big data in chunks and it makes loading very easy and faster.

You can see the pagination on any website with records by the page numbers like 1, 2, 3 .. and Next and previous button.

Reduce roundtrips by Server.Transfer:

Roundtrips can be reduced by using Server. Transfer. If you want to transfer a page with in the same server you can use Server.Transfer to tranfer page. It reduces roundtrips between server and browser but do not update browser history.

Background process using threads:

Threads are the core of parallel programming. To use system resources effictively threads are very important to use. Processes can be run in background in the new thread and we can send back response to the client side. So we do not have to wait untile the process finish so function can send back the response.

Let suppose we have to send emails to multiple users so we have to just click on send button and we will get the success response immidiately when the emails can not be sent to the large number of users immidiately so what happened? Email sending process will run in background.

Thank you for reading. Visit Mycodebit.com for .net core and .net tutorial.

Leave a Reply

Your email address will not be published.