Removing or Customizing View Engines in MVC

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

If you are not using any view engine like ASPX View Engine, better you remove it to improve the performance, it is one of the MVC performance tuning tips from the bunch.

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 view for this action method now.

public ActionResult Foo()
{
    return View();
}

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


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

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

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();
}

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


You can see 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.

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

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.App_Start
{
    public class CustomRazorViewEngine : RazorViewEngine
    {
        public CustomRazorViewEngine()
        {
            base.AreaViewLocationFormats = new string[] {
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml"
            };

            base.AreaMasterLocationFormats = new string[] {
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml"
            };

            base.AreaPartialViewLocationFormats = new string[] {
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml"
            };

            base.ViewLocationFormats = new string[] {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml"
            };

            base.PartialViewLocationFormats = new string[] {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml"
            };

            base.MasterLocationFormats = new string[] {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml"
            };
        }
    }
}

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

protected void Application_Start()
{
    ViewEngines.Engines.Clear();
    //ViewEngines.Engines.Add(new RazorViewEngine());
    ViewEngines.Engines.Add(new CustomRazorViewEngine());

    AreaRegistration.RegisterAllAreas();

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

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


Super cool.
Hope this helps.

Comments

Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System