Remove ViewEngines that are not used : An ASP.NET MVC performance tip

Today, I am writing a small post on ASP.NET MVC that will help in performance of ASP.NET MVC application. So lets discuss it in detail.

Whenever we create an ASP.NET MVC application it allows us to select a View Engine Razor for the application as.

select View engine

Here we selected Razor and this is selected by default as well.

But when applications run, it loads multiple view engines and we use only one view engine normally that is selected while application creation. We can prove it many ways

1) Let’s see the collection ViewEngines in Global.asax. It shows as while debugging.

firstallThe above clearly shows that it contains two View Engines while we selected Razor View Engine while creating application.

2) To prove it another way, As we know when we use Razor View Engine then views extension is .cshtml and for WebForms view engine, it is .aspx. So lets say we created a controller and it has a method like

public ActionResult Index()
    return View();

And we did not created any view accordingly. Now when we run the applications it throws the following error


It tries to find the View but could not find. It also shows what file names and where does it try to find. If we see the above screenshot then we find that it tries to look files with extension .cshtml/.vbhtml and .aspx/.ascx as well, where .aspx/.ascx is used in case of web-forms view engine. And we selected razor View engine while application creation. It simply shows that it loads two view  engines which is not required.

Solution : Add the following code in the method Application_Start in Global.asax

// Removes all the view engine

// Add View Engine that we are ging to use. Here I am using Razor View Engine
ViewEngines.Engines.Add(new RazorViewEngine());

Now after making the above changes, it loads only the added ( here Razor) view engine.

Now we can check the scenario discussed in point 2 and now see the page

afterremovinNow it tries to find only .cshtml/.vbhtml files.

We should use the above logic and because there is no use loading all the view engines in memory if we are not using .

Hope you all find it useful and use it in your projects.



2 thoughts on “Remove ViewEngines that are not used : An ASP.NET MVC performance tip

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s