Monday, 30 January 2017

Can you remove default View Engine in ASP.NET MVC? How?

We can customize view engines in ASP.NET MVC application.

If you are not using any view engine like ASPX View Engine, better you remove it to improve the performance, it is one of the MVC performance tuning tips from the bunch.

Removing the Web Form view engine is easy in MVC. We can remove all the view engines and add only Razor view engine by using Application_Start event of Global.asax.cs file like below.

  1. protected void Application_Start()
  2. {
  3. //Remove All Engine
  4. ViewEngines.Engines.Clear();
  5. //Add Razor Engine
  6. ViewEngines.Engines.Add(new RazorViewEngine());
  7. ...
  8. }

NOTE:
  1. After removing Web Form engine, Razor View Engine will be almost twice as fast with the Web Form engine.
  2. Use this when you are sure that you will use only Razor views. It will be helpful to you.
  3. If you are using both type of views (ASPX & Razor), don't implement this.
You can also customize the view engines. for more click 

Tuesday, 27 December 2016

Different methods used to render the views and Partial views in ASP.Net MVC


1. RenderPartial  - RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model.
@{Html.RenderPartial("_partialView");}
This method returns void. And simple to use and no need to create any action.
This method is faster than Partial method since its result is directly written to the response stream which makes it fast.

2. Partial - Like RenderPartial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model.
@Html.Partial("_partialView")
Simple to use and no need to create any action.This method result can be stored in a variable, since it returns string type value.
Renders the partial view as an HTML-encoded string.

3. RenderAction - RenderAction method is useful when the displaying data in the partial view is independent from corresponding view model.
@{Html.RenderAction("Action_Name","Controller-Name");}
For this method, we need to create a child action for the rendering the partial view.
This method is the best choice when you want to cache a partial view.
This method is faster than Action method since its result is directly written to the HTTP response stream which makes it fast.

4. Action - Action method is useful when the displaying data in the partial view is independent from corresponding view model.
@Html.Action("Action_Name","Controller-Name");
For this method, we need to create a child action for the rendering the partial view.This method result can be stored in a variable, since it returns string type value.
Renders the partial view as an HtmlString .

This method is also the best choice when you want to cache a partial view.

5. Direct return from Controller 


public ActionResult Action()
        {
            return PartialView("PartialViewName");
OR
            return View("ViewName");

        }

For More check Views Partial Views