Remove Unused View Engine Sample in MVC: Day 37

To resolve a view, MVC searches for a web form (ASPX) view engine and razor view engine as in its naming convention. So when we do not add a view and try to run the project, it searches for both of the engines. For example if we use the razor engine in our entire project then why do we want MVC to search for web form view engine? So in this example we remove the unused view engine, in other words the web form view engine and improve the performance of our application.

The following is the procedure to do that.
  •  Create a MVC project from the "Empty" template.

    Right-click on "Controllers" and select "Add" >> "Controller...".

    Empty

  • Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button.

    MVC5Controller

  •  Name the controller as in the following:

    HomeController

  • We have an Index action result method that returns a view, Index.cshtml. But do not add a view for this action result.

    IndexCshtml
     
  • Now run the project and you will see that ASP.NET MVC checks for a series of conventions that are built in. It searches for a web form view engine and razor view engine.

    ServerError

  • Now let's remove the web form view engine.

    In the Global.asax.cs file, remove all the view engines using the ViewEngines.Engines.Clear method.

    Add RazorViewEngine using the ViewEngines.Engines.Add method.

    GlobalAxax

  • Rerun the project and you will see ASP.NET MVC searches for razor view engines only.

    ServerError2
Note: If you use both of the view engines (web form and razor) in your project then don't implement the code above.

<< Custom Action Result Sample in MVC: Day 36


Similar Articles