dcsimg
 

Best Practices for Improving Web Application Performance

Tuesday May 15th 2018 by Tapas Pal

Take advantage of the components listed in this article to improve the performance of an ASP.NET Web application.

Introduction

Improving Web application performance is critical. Users wants a more lightweight application which runs quicker and gives a better response time. In this article, I will suggest a few tips to improve the performance of an ASP.NET Web application by taking advantage of the following components.

Implement Caching

Caching is a good technique for improving your application's performance. You can cache entire pages or fragments of pages or controls, depending on the logic involved. The best way to implement a cache on an MVC view is to add an [OutputCache] attribute to an individual controller action or an entire controller class. The following code snippet shows the controller action Index () that will be cached for 30 seconds.

[OutputCache(Duration = 30, VaryByParam = "none", Location =
   System.Web.UI.OutputCacheLocation.Client)]
public ActionResult Index()
{

}

To cache your entire controller, add an [OutputCache] attribute on an entire controller class, as shown below.

[OutputCache(Duration = 15, VaryByParam = "None")]
   public class AuthorizationContext
   {
      public ActionResult Index()
      {
         return View();

      }

      public ActionResult GetDetails(string Id)
      {

      }
   }

By using the System.Web.Caching.Cache class, you can add key and item pairs to the Cache class. The added item is cached on the server. The following code example adds an item to the cache and sets an expiration time of five minutes.

Cache.Insert("Key", "Value",
   null, System.Web.Caching.Cache.NoAbsoluteExpiration,
   new TimeSpan(0, 5, 0));

Disable View State if Not Required

Increasing the number of objects added into the view state also increases the size of view state dictionary. It hits your performance negatively because the processing time required to serialize and to deserialize the objects increases. By default, view state is turned on in an ASP.NET application. Disable view state if you do not need it. To disable view state for your application, configure the <pages> element in the web.config file as follows.

<pages enableViewState="false" />

Follow this syntax to disable view state for a single control on a page:

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

Reduce Round Trips

Developers have to make sure that round trips are at an absolute minimum. In any Web application, loading data from a database should avoid unnecessary database hits to load the unchanged content in the database. Also, connection pooling can help to increase performance by reducing network traffic and processing the load on the database server. Developers can implement an Ajax UI whenever possible. Ajax partially posts back to avoid a full page refresh and only updates the portion of the page that needs to be changed.

Minify Resources (HTML, CSS, and JavaScript)

By using minification, we can remove white space characters, new line characters, comments, and so forth from code. Minifying the source code speeds up page loading and improves Web application performance. It's always recommended to use the minified versions of JavaScript and CSS files in production.

Use Bundling

Bundling is a technique to easily combine multiple files into a single file. A single HTTP request is enough to bring all the code in the bundled files. Bundling was introduced by Microsoft in the ASP.NET 4.5 framework. The following code snippet shows an example of adding multiple JavaScript and CSS files in a bundle.

using System.Web.Optimization;
namespace BundleExample
{
   public class MyBundle
   {
      public static void RegisterBundle(BundleCollection bundle)
      {
         bundle.Add(new ScriptBundle("~/bundles/AllJSFiles")
         .Include("~/include/js/General.js",
         "~/include/js/Validation.js",
         "~/include/js/Others.js"));

         bundle.Add(new StyleBundle("~/bundles/AllCSSFiles")
         .Include("~/include/style/main.css",
         "~/include/Style/Container.css",
         "~/include/Style/Others.css"));
         BundleTable.EnableOptimizations = true;
      }
   }
}

Use Pagination

If your application is loading tons of data from a database, it's advisable to take advantage of the paging simplicity provided in .NET. Loading a large number of records from the database into the server data controls—such as GridView, DataGrid, and ListView—will take a lot of time and it will delay the page's loading. During design time, a developer has to plan to show only small subsets of data at load time to make page loading faster. Pagination is a simple technique to bring in chunks of data on a click of the Next button, Previous button, or on the page number.

Set Build Action to Release

Only non-production builds should be pushed in debug mode. All production builds are in release mode instead of debug mode because the debug building mode adds a .pdb file for each DLL and this will cause an increase of the application's size; execution time will increase, too. During a debug build, debug information is checked before executing code in these assemblies.

Figure 1 shows how you set the build action using the release mode.

Setting the build action
Figure 1: Setting the build action

Use Server.Transfer Instead of Response.Redirect

Use Server.Transfer instead of Response.Redirect to reduce back-and-forth network traffic, which therefore eases the pressure on your Web server and network bandwidth. It also makes your applications run faster.

Conclusion

Apart from the preceding best practices, a Content Delivery Network (CDN) also helps increase performance. Keep your static contents in a CDN (a network of servers spread across different geographies of the world). During execution, the application gets its resources from a node which is geographically nearest to the user. A CDN ensures faster response time during content delivery.

Home
Mobile Site | Full Site