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.
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.
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.
- protected override void OnApplicationStarted()
- {
- ViewEngines.Engines.Clear();
- ViewEngines.Engines.Add(new RazorViewEngine());
- base.OnApplicationStarted();
-
-
- AreaRegistration.RegisterAllAreas();
-
- RegisterGlobalFilters(GlobalFilters.Filters);
- RegisterRoutes(RouteTable.Routes);
- RegisterBundles(BundleTable.Bundles);
- Register(GlobalConfiguration.Configuration);
- RegisterAuth();
- System.Threading.LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
- }
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.
- public static void RegisterBundles(BundleCollection bundles)
- {
- bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
- "~/Content/Jquery/jquery-{version}.js"));
-
- bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
- "~/Content/Jquery/jquery.unobtrusive*",
- "~/Content/Jquery/jquery.validate*"));
-
-
- bundles.Add(new ScriptBundle("~/bundles/JqueryHomePage").Include("~/Content/js/jquery-1.8.3.min.js", "~/Content/Jquery/bootstrap.min.js", "~/Content/Jquery/HomeJquery.js"));
-
- bundles.Add(new StyleBundle("~/Content/HomeMain").Include("~/Content/Css/style.css").Include("~/Content/Css/font-awesome/css/font-awesome.css", new CssRewriteUrlTransform()));
-
- }
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.
- public class HomeController : Controller
- {
- [OutputCache(Duration = 300, VaryByParam = "none")]
- public ActionResult Index()
- {
- return View();
- }
- }
You need to make some changes in Web.Config to set the application in Cache mode.
- <configuration>
- <system.webserver>
- <staticcontent>
- <clientcache cachecontrolmode = "UserMaxAge" httpexpires="365.00:00:00">
- </clientcache></staticcontent>
- </system.webserver>
- </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.
Or simply using the Web Essentials feature, you can make images as sprite images.
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.
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.
- Dotnet Memory Profiler
- Fiddler
- FireBug
- Google Page Speed Test
- Web Page Test
Thanks for reading the article, hope you enjoyed it.