Customizing View Engines in Asp.Net

Introduction

This article describes the customization of a view engine or adding a custom view engine. Let's see the search process during the loading of a webpage reducing by folds that somehow makes our app optimized.

Why Really is it Needed

When we forget to add a View page to any action method in the controller, we get an error like:

ti1error

Just imagine that though some milliseconds, the searching of all these is worthless if we are using a specific View Engine, may it be Razor or Aspx in C# or VB.

Using the Code

It's very simple and easy. Let's peep into the code.

Step 1: Go to the Global.asax.

Global.asax is the heart of the application since it contains the App_start() method that starts our application.

  1. protected void Application_Start()  
  2. {  
  3.    AreaRegistration.RegisterAllAreas();  
  4.    WebApiConfig.Register(GlobalConfiguration.Configuration);  
  5.    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  6.    RouteConfig.RegisterRoutes(RouteTable.Routes);  
  7.    BundleConfig.RegisterBundles(BundleTable.Bundles);  
  8.    AuthConfig.RegisterAuth();  
  9. }  
Step 2: Add to the Application_Start() method
  1. protected void Application_Start()  
  2. {  
  3.    ViewEngines.Engines.Clear();//This clears all the Web form view engines.  
  4. }  
After this, when we run we get an error page denoting:

get an error page

Step 3: After this Add
  1. protected void Application_Start()  
  2. {  
  3.    ViewEngines.Engines.Clear(); //This clears all the view engines as mentioned earlier  
  4.    ViewEngines.Engines.Add(new RazorViewEngine()); //This RazorViewEngine() is present in the System.Web.Mvc namespace  
  5. }  
Here after this, we get an error page denoting:

error page

This is because we have added only RazorViewEngine(), that has the extension cshtml (for C#) and vbhtml (for VB). Just see that the search has been reduced in half.

Step 4: Now we create our own custom View Engine

Add a class to the App_Start folder (with any name that suits you). Then make the class inherit from RazorViewEngine.
  1. public class "your viewengine   
  2.   
  3. name":RazorViewEngine //This RazorViewEngine in present in the System.Web.Mvc namespace  
Add the following code into the class:
  1. ViewLocationFormats = new[]  
  2. {  
  3.    "~/Views/{1}/{0}.cshtml",  
  4.    "~/Views/Shared/{0}.cshtml"  
  5. };  
  6. MasterLocationFormats = new[]  
  7. {  
  8.    "~/Views/{1}/{0}.cshtml",  
  9.    "~/Views/Shared/{0}.cshtml"  
  10. };  
We can similarly add for partialviews too (PartialViewLocationFormats = new...).

This is your custom ViewEngine before building and running your app. Don't forget to add:
  1. ViewEngines.Engines.Add(new your viewengine name());  
This helps your search by reducing it by folds from 8 to 2. That is: now only 2 are searched.

error

Conclusion

After removing the Webform Engines, the Razor View Engine will be twice as fast as the Webform Engines.

I hope this helps beginners like me. :). 


Similar Articles
Invincix Solutions Private limited
Every INVINCIAN will keep their feet grounded, to ensure, your head is in the cloud