Please enable Javascript to correctly display the contents on Dot Net Tricks!

Custom Razor View Engine for C# and VB

  Author : Shailendra Chauhan
Updated On : 26 Sep 2016
Total Views : 129,705   
Support : MVC4 & MVC3
 

You should be happy to know, Asp.Net MVC is an open source and highly extensible framework. You can customized it according to your need. As you read my previous article Removing the Web Form View Engine for better performance of Razor View Engine from your Asp.Net MVC Razor application. In this article, you will learn how can you customize the Razor View engine for C# and VB language.

Removing All the View Engines & registering the Razor Engine

protected void Application_Start() 
{ 
 //Remove All View Engine including Webform and Razor
 ViewEngines.Engines.Clear();
 //Register Razor View Engine
 ViewEngines.Engines.Add(new RazorViewEngine());
 //Other code is removed for clarity
} 

After removing the Web Form and other View engine as you noticed that Razor View engine looks up the C# and VB views as shown below.

As you know MVC is highly extensible hence you can completely replace the Razor view engine with a new custom razor engine. by doing so, this will slightly improve your application performance. If your MVC application is using only C# language, there is no need to looks up the .vbhtml views and same for VB language.

Custom C# Razor View Engine

public class CSharpRazorViewEngine : RazorViewEngine
{
 public CSharpRazorViewEngine()
 {
 AreaViewLocationFormats = new[]
 {
 "~/Areas/{2}/Views/{1}/{0}.cshtml",
 "~/Areas/{2}/Views/Shared/{0}.cshtml"
 };
 AreaMasterLocationFormats = new[]
 {
 "~/Areas/{2}/Views/{1}/{0}.cshtml",
 "~/Areas/{2}/Views/Shared/{0}.cshtml"
 };
 AreaPartialViewLocationFormats = new[]
 {
 "~/Areas/{2}/Views/{1}/{0}.cshtml",
 "~/Areas/{2}/Views/Shared/{0}.cshtml"
 };
 ViewLocationFormats = new[]
 {
 "~/Views/{1}/{0}.cshtml",
 "~/Views/Shared/{0}.cshtml"
 };
 MasterLocationFormats = new[]
 {
 "~/Views/{1}/{0}.cshtml",
 "~/Views/Shared/{0}.cshtml"
 };
 PartialViewLocationFormats = new[]
 {
 "~/Views/{1}/{0}.cshtml",
 "~/Views/Shared/{0}.cshtml"
 };
 }
} 

Regisering the C# Razor View Engine

protected void Application_Start() 
{ 
 //Remove All View Engine including Webform and Razor
 ViewEngines.Engines.Clear();
 //Register C# Razor View Engine
 ViewEngines.Engines.Add(new CSharpRazorViewEngine());
 //Other code is removed for clarity
} 

Custom VB Razor View Engine

Imports System.Web.Mvc
Public Class VBRazorViewEngine Inherits RazorViewEngine
 Public Sub New()
 AreaViewLocationFormats = {
 "~/Areas/{2}/Views/{1}/{0}.vbhtml", 
 "~/Areas/{2}/Views/Shared/{0}.vbhtml"
 }
 AreaMasterLocationFormats = {
 "~/Areas/{2}/Views/{1}/{0}.vbhtml", 
 "~/Areas/{2}/Views/Shared/{0}.vbhtml"
 }
 AreaPartialViewLocationFormats = {
 "~/Areas/{2}/Views/{1}/{0}.vbhtml", 
 "~/Areas/{2}/Views/Shared/{0}.vbhtml"
 }
 ViewLocationFormats = {
 "~/Views/{1}/{0}.vbhtml", 
 "~/Views/Shared/{0}.vbhtml"
 }
 MasterLocationFormats = {
 "~/Views/{1}/{0}.vbhtml", 
 "~/Views/Shared/{0}.vbhtml"
 }
 PartialViewLocationFormats = {
 "~/Views/{1}/{0}.vbhtml", 
 "~/Views/Shared/{0}.vbhtml"
 }
 End Sub
End Class

Regisering the VB Razor View Engine

Sub Application_Start()

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

End Sub

Note

  1. Use one of the approach when you are sure that you will use only either C# language or VB Language. It will be helpful to you.

  2. If you are using both type of views (cshtml and vbhtml), don't implement this.

What do you think?

I hope you will enjoy the tricks while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

Know More
15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

14 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

3 JAN
ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

5 NOV
ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

4 SEP
MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+