How to speed up ASP.NET MVC view rendering process?

It can be very disappointing if you later figure out that your application is serving requests very slow when deployed in your production environment. There can be various reasons that can make this happen. In this post, I’ll tell you how to speed up ASP.NET MVC view rendering process.

Your application may have different issues that can slow down the response time to a request. One easy way to check is to use a profiler like MiniProfiler or Glimpse for your ASP.NET MVC application.

I’ll not go in detail here about how to use these profilers. Their documentation is quite straightforward as both are an easy plug and play solution which can be used to profile or see response times at different stages of request. I like Glimpse and I’ve been using it for a long time.

Views in ASP.NET MVC are run by ViewEngines to create HTML that browser can understand easily. By default, there are two view-engines supported: – Razor (System.Web.Razor) and WebForm (System.Web.Mvc.WebFormViewEngine). Check this link to see the comparison between these two engines.

It’s high likely that you will use one or the other in your application. If it is a new application, I believe you will choose Razor as view-engine over other because of the good syntactic goodies it provides for developers.

When it comes to the framework to find the correct view to use for a request, it then tries to match up the specific view-name with .cshtml and .aspx file extensions. This can be time-consuming and actually not needed as most probably you will end up using either of one. If you are using both the view-engines in you app, you better have a valid reason to do so!

We can easily instruct framework to not to look for other unwanted view file extensions when the app starts. The addition of following code in Startup.cs file’s configuration method can help us to improve the response time:

It first clears out all the registered view-engines. Then we only add the one that our application needs. In my case, it is RazorViewEngine. Make sure to add cshtml to instruct the framework to only look for files with this extension.

When you deploy your application, you must use the Release mode and not Debug mode. This is really important as the framework applies some aggressive caching when you app runs in release mode. So, make sure the Compilation element in web.config file looks like this:

Below is the piece of code from the framework that uses caching only in certain conditions:

The highlighted code is the reason why is it really important to run your application in release mode in production environment.

Now you may have a question that you can specify a view name with actual path and extension in the view action result, for e.g.: return View(@"~/Views.Home/Index.cshtml"). But sadly, this will not help you. Read this post to see the benchmarks in detail.

This sums up my post to tell you how to speed up ASP.NET MVC  view rendering process. This can definitely help you to improve response time but watch out for other possible issues.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger