Removing unused View Engines in MVC

 

In this article we will learn how to remove View Engines which is not being used by 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:

  •  In default, MVC will search 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 like below

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, by given code 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 :

Removing View Engines in MVC 3