Click here to Skip to main content
Click here to Skip to main content

Customizing View Engines

, 6 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Customizing the view Engines (Razor View Engine) in MVC4 to optimize our App.

Introduction

Customizing the View Engines or adding our custom View Engines lets the search process during the loading of out webpage reduce 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:

Just imagine though some milliseconds but searching 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 codes.

Step 1: Go to the Global.asax

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

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    AuthConfig.RegisterAuth();
}

Step 2: Add to the Application_Start() method

protected void Application_Start()
{
    ViewEngines.Engines.Clear();//This clears all the Web form view engines.
}

After this, when we run we get an error page denoting:

Step 3: After this Add

protected void Application_Start()
{
    ViewEngines.Engines.Clear(); //This clears all the view engines as mentioned earlier
    ViewEngines.Engines.Add(new RazorViewEngine()); //This RazorViewEngine() is present in the System.Web.Mvc namespace
}

Here after this, we get an error page denoting:

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

Step 4: Now we create our own custom View Engine

--Add a class to App_Start folder (with any name that suits you). Then make the class inherit from RazorViewEngine.

public class "your viewengine 
name":RazorViewEngine //This RazorViewEngine in present in the System.Web.Mvc namespace

--Add the following code into the class:

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

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:

ViewEngines.Engines.Add(new your viewengine name());

This helps your search reduced by folds from 8 to 2. That is: now only 2 are searched.

Conclusion

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

Hope this helps beginners like me. Smile | :)

History

  • 6 March 2014 - First version.
  • 7 March 2014 - Formatting updates.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Suraj Sahoo | Coding Passion
Software Developer (Junior) Mindfire Solutions
India India

I am the son of Mr. Debendra Nath Sahoo & Dr. Sanjulata Sahoo, felt worth mentioning them as they are the reason I am being able to key these words to the world.

I am currently a Software Developer at Mindfire Solutions,India.Working currently on ASP.NET MVC4 Web Applications.



I am passionate and enthusiastic to learn more about softwares and coding.Code Project is the best platform to feed me what i want.



I would love to know queries and questions from all of you specially beginners like me so that we could share some Code stuffs and make new recipes.Drop into suraj.0241@gmail.com.

My Own Blog

Suraj | Coding Passion

Achievements

I am Microsoft MVC4(70-486) certified.

 

Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 7 Mar 2014
Article Copyright 2014 by Suraj Sahoo | Coding Passion
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid