8 Steps To Make Your ASP.NET MVC Website Faster

As we know that performance is an important part for every application on production. If any website is facing issue of performance then the users do not prefer to use that website. There are many ways to improve the performance of a website in ASP.NET MVC. I am going to demonstrate one by one.

Step 1: Release Mode Build

You should always use the debug build on production. You should make sure that application is compiled in Release Mode rather than Debug Mode.

Release Mode

You need to make changes in Web.Config on Production, when going to deploy the ASP.NET MVC application. Make sure compilation debug mode should be false.

Debug Mode

Step 2: Use Single View Engine

As you know in ASP.NET MVC, we can use multiple View Engines; it can be Web Forms [Aspx] orRazor [cshtml] or you can also create your own custom engine to process the ASP.NET MVC Views. We can also configure all engines in same application.

You need to care two things before setting up the engine for the MVC application. Firstly, Engines list should be clear before setting up new engine and only one engine should be setting up.

  1. protected override void OnApplicationStarted()  
  2. {  
  3.     ViewEngines.Engines.Clear();  
  4.     ViewEngines.Engines.Add(new RazorViewEngine());  
  5.     base.OnApplicationStarted();  
  6.   
  7.   
  8.     AreaRegistration.RegisterAllAreas();  
  9.   
  10.     RegisterGlobalFilters(GlobalFilters.Filters);  
  11.     RegisterRoutes(RouteTable.Routes);  
  12.     RegisterBundles(BundleTable.Bundles);  
  13.     Register(GlobalConfiguration.Configuration);  
  14.     RegisterAuth();  
  15.     System.Threading.LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);  
  16. }  
Step 3: Bundling and Minification

We used to create client side application with attractive design. So, for this we always prefer to use Jquery/Javascript and CSS. But as we all know these files are downloaded when we are going to access the website. If you are using multiple CSS and Jquery file then it will take more time to load it and it makes your site very slow.

So, you should prefer to use small file which can be loaded faster or single file which load faster rather than multiple file. In MVC there are two concepts behind this Bundling and Minification. This will help your site to make it faster and decrease the load time.
  1. public static void RegisterBundles(BundleCollection bundles)  
  2. {  
  3.     bundles.Add(new ScriptBundle("~/bundles/jquery").Include(  
  4.                "~/Content/Jquery/jquery-{version}.js"));  
  5.   
  6.     bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(  
  7.                "~/Content/Jquery/jquery.unobtrusive*",  
  8.                "~/Content/Jquery/jquery.validate*"));  
  9.       
  10.   
  11.     bundles.Add(new ScriptBundle("~/bundles/JqueryHomePage").Include("~/Content/js/jquery-1.8.3.min.js""~/Content/Jquery/bootstrap.min.js""~/Content/Jquery/HomeJquery.js"));  
  12.       
  13.     bundles.Add(new StyleBundle("~/Content/HomeMain").Include("~/Content/Css/style.css").Include("~/Content/Css/font-awesome/css/font-awesome.css"new CssRewriteUrlTransform()));  
  14.   
  15. }  
RegisterBundles

Web essentials

Step 4: Caching Enabled

Caching has an important role to make the website faster. It increases the performance smoothly. Actually when website uses the Caching features, it provides the facility to use the existing and downloaded file for next process. Using Caching, you can save some kind of files on server memory and use it next time without downloading the new one.
  1. public class HomeController : Controller  
  2. {  
  3.   [OutputCache(Duration = 300, VaryByParam = "none")] //cached for 300 seconds  
  4.   public ActionResult Index()  
  5.   {  
  6.       return View();  
  7.   }  
  8. }  
You need to make some changes in Web.Config to set the application in Cache mode.
  1. <configuration>  
  2.   <system.webserver>  
  3.     <staticcontent>  
  4.       <clientcache cachecontrolmode = "UserMaxAge" httpexpires="365.00:00:00">  
  5.     </clientcache></staticcontent>  
  6.   </system.webserver>  
  7. </configuration>  
Step 5: Load Minimum Data

Now a days, mostly site connect with database for rendering the data on View. But as we know if you are going to load heavy data one time, it makes your site slow. So, you should always prefer to bring the small amount of the data one time and render it on the View.

The easiest way to do this is to use Paging with your application and show the minimum data one time, this will help your site to increase performance. You can use Stored Procedure to get the data which is the fastest way and also you can add filtering option in it.

Step 6: Use Sprite Images

Images make your website more beautiful and user friendly. I believe, images help us to understand the concepts easily. But if there are multiple images on single page, it will affect the performance of the site. It is because images take time to download.

So, here you can use Sprite Image which combines the multiple images in one and load in single download. You can install Sprite Image generator with your application.

 Sprite Image

Or simply using the Web Essentials feature, you can make images as sprite images.

Web Essentials feature

Step 7: Client Side Validation


Validation takes important part because it validates the user data that it is valid or not. But suppose after validating the single data, your site is going to load that means user should wait every time to load your site again and again. So, always prefer to make the validation on Client Side.

It makes your site faster because there is no need to load the site on every input validation. It will show error message if something goes wrong on client side.

Client Side Validation

Step 8: Use Performance Tools

You can do many things to make your site faster. But sometime, we are confused and unable to figure out when my site is taking much time to load or sometimes there are memory leaks issue. So, in that scenario you can use some tools which can help you to figure out the root causes.
  1. Dotnet Memory Profiler
  2. Fiddler
  3. FireBug
  4. Google Page Speed Test
  5. Web Page Test

Use Performance Tools

Thanks for reading the article, hope you enjoyed it.


Similar Articles