Removing unused View Engines in MVC

In this post, we will discuss how to remove View Engines which is not being used by the application.

Why need to remove?

If we remove View Engines which is not being used by application, then it lead to improve the performance in our application.

Discussion:

  •  As default, ASP .NET MVC will search for the webform (.aspx) view engine first  to find the match  naming conventions. After that it start to search for the views that match the Razor view engine naming conventions.

Example : 1

Here i am creating a new controller with default action, but i don’t add view for this action method.

public class SampleController : Controller
{
public ActionResult Index()
{
return View();
}

}

In this situation i am try to run the application to access the sample controller, then it will show output as shown below

Removing View Engines in MVC 4

Example : 2

Let we  removing  the view engines and register only Razor view engine like below

protected void Application_Start()
{

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());

AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}

Explanation:

  • It can control by placing it in Global.asax file.
  • All Engine will be clear by using ViewEngines.Engines.Clear().
  • ViewEngines.Engines.Add(new RazorViewEngine()) it will again register razor view engine to the application

Output :

 

2 thoughts on “Removing unused View Engines in MVC”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.