Removing or Customizing View Engines in MVC

In this article, you will learn how to remove or customize View Engines not being used by an application.

If you are not using any view engine like ASPX View Engine, it is better to remove it to improve the performance, it is one of the many MVC performance tuning tips.
 
You might be wondering how it will improve the performance. Let's prove it by creating a new action method in a Home Controller, don't add a view for this action method now. 

  1. public ActionResult Foo()  
  2. {  
  3.      return View();  
  4. }   

Now, run the application and try navigating to "http://localhost:1212/Home/Foo". Here is what I received:

MVC1.png

In the image above you can see how the MVC runtime is looking for an ASPX View Engine first and then the other view engines, which is still a default behavior.

You can control it from the Global.asax file by taking advantage of ViewEngines.Engines.Clear().

  1. protected void Application_Start()  
  2. {  
  3.        ViewEngines.Engines.Clear();  
  4.        ViewEngines.Engines.Add(new RazorViewEngine());  
  5.        AreaRegistration.RegisterAllAreas();  
  6.        WebApiConfig.Register(GlobalConfiguration.Configuration);  
  7.        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  8.        RouteConfig.RegisterRoutes(RouteTable.Routes);  
  9.        BundleConfig.RegisterBundles(BundleTable.Bundles);  
  10.        AuthConfig.RegisterAuth();  
  11.  } 

I highlighted the newly added code above. Now, run the application; you will see the following:

MVC2.png

You will see that the MVC runtime successfully removed the ASPX View Engine but it is still looking for VBHTML Views.

You can also control it by customizing Razor View Engine to use only C# languages, by making some changes here:

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

I highlighted the portion of code. This is actually calling the RazorViewEngine. RazorViewEngine() method internally that has support for both languages (C# and VB) by default. So, we need to override the default functionality by adding a class by the name "CustomRazorViewEngine" that inherits RazorViewEngine. The best place to add this class file is in the App_Start folder.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace MvcApplication1.App_Start  
  7. {  
  8.     public class CustomRazorViewEngine : RazorViewEngine  
  9.     {  
  10.         public CustomRazorViewEngine()  
  11.         {  
  12.             base.AreaViewLocationFormats = new string[] {  
  13.                 "~/Areas/{2}/Views/{1}/{0}.cshtml",  
  14.                 "~/Areas/{2}/Views/Shared/{0}.cshtml"  
  15.             };  
  16.             base.AreaMasterLocationFormats = new string[] {  
  17.                 "~/Areas/{2}/Views/{1}/{0}.cshtml",  
  18.                 "~/Areas/{2}/Views/Shared/{0}.cshtml"  
  19.             };  
  20.             base.AreaPartialViewLocationFormats = new string[] {  
  21.                 "~/Areas/{2}/Views/{1}/{0}.cshtml",  
  22.                 "~/Areas/{2}/Views/Shared/{0}.cshtml"  
  23.             };  
  24.             base.ViewLocationFormats = new string[] {  
  25.                 "~/Views/{1}/{0}.cshtml",  
  26.                 "~/Views/Shared/{0}.cshtml"  
  27.             };  
  28.             base.PartialViewLocationFormats = new string[] {  
  29.                 "~/Views/{1}/{0}.cshtml",  
  30.                 "~/Views/Shared/{0}.cshtml"  
  31.             };  
  32.             base.MasterLocationFormats = new string[] {  
  33.                 "~/Views/{1}/{0}.cshtml",  
  34.                 "~/Views/Shared/{0}.cshtml"  
  35.             };  
  36.         }  
  37.     }  
  38. } 

Remember to use the System.Web.Mvc namespace. Now, in the Global.asax file add the following code:

  1. protected void Application_Start()  
  2. {  
  3.     ViewEngines.Engines.Clear();  
  4.     //ViewEngines.Engines.Add(new RazorViewEngine());  
  5.     ViewEngines.Engines.Add(new CustomRazorViewEngine());  
  6.     AreaRegistration.RegisterAllAreas();  
  7.     WebApiConfig.Register(GlobalConfiguration.Configuration);  
  8.     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  9.     RouteConfig.RegisterRoutes(RouteTable.Routes);  
  10.     BundleConfig.RegisterBundles(BundleTable.Bundles);  
  11.     AuthConfig.RegisterAuth();  
  12. } 

Remember to use the MvcApplication1.App_Start namespace to bring the class file created above into the Global.asax scope. Now, run the application, you will see the following output:

MVC3.png


Similar Articles