HI WELCOME TO SIRIS

Custom Razor View Engine for C# and VB

You should be happy to know, Asp.Net MVC is an open source and highly extensible framework. You can customize 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

  1. protected void Application_Start()
  2. {
  3. //Remove All View Engine including Webform and Razor
  4. ViewEngines.Engines.Clear();
  5. //Register Razor View Engine
  6. ViewEngines.Engines.Add(new RazorViewEngine());
  7. //Other code is removed for clarity
  8. }
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

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

Regisering the C# Razor View Engine

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

Custom VB Razor View Engine

  1. Imports System.Web.Mvc
  2. Public Class VBRazorViewEngine Inherits RazorViewEngine
  3. Public Sub New()
  4. AreaViewLocationFormats = {
  5. "~/Areas/{2}/Views/{1}/{0}.vbhtml",
  6. "~/Areas/{2}/Views/Shared/{0}.vbhtml"
  7. }
  8. AreaMasterLocationFormats = {
  9. "~/Areas/{2}/Views/{1}/{0}.vbhtml",
  10. "~/Areas/{2}/Views/Shared/{0}.vbhtml"
  11. }
  12. AreaPartialViewLocationFormats = {
  13. "~/Areas/{2}/Views/{1}/{0}.vbhtml",
  14. "~/Areas/{2}/Views/Shared/{0}.vbhtml"
  15. }
  16. ViewLocationFormats = {
  17. "~/Views/{1}/{0}.vbhtml",
  18. "~/Views/Shared/{0}.vbhtml"
  19. }
  20. MasterLocationFormats = {
  21. "~/Views/{1}/{0}.vbhtml",
  22. "~/Views/Shared/{0}.vbhtml"
  23. }
  24. PartialViewLocationFormats = {
  25. "~/Views/{1}/{0}.vbhtml",
  26. "~/Views/Shared/{0}.vbhtml"
  27. }
  28. End Sub
  29. End Class

Regisering the VB Razor View Engine

  1. Sub Application_Start()
  2.  
  3. ViewEngines.Engines.Clear();
  4. ViewEngines.Engines.Add(new VBRazorViewEngine());
  5.  
  6. 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.