Part - 6 : ASP.NET MVC Interview Question's Answer

26. How View Engine works?

Each view engine has following three main components:
  1. ViewEngine class - This class implements the IViewEngine interface and responsible for locating view templates.
  2. View class - This class implements the IView interface and responsible for combining the template with data from the current context and convert it to output HTML markup.
  3. Template parsing engine - This parses the template and compiles the view into executable code.

27. What is Razor View Engine?

Razor Engine is an advanced view engine that was introduced with MVC3. This is not a new language but it is a new markup syntax. Razor has new and advance syntax that are compact, expressive and reduces typing.Razor syntax are easy to learn and much clean than Web Form syntax. Razor uses @ symbol to write markup as:
 @Html.ActionLink("SignUp", "SignUp")

28. How to make Custom View Engine?

ASP.NET MVC is an open source and highly extensible framework. You can create your own View engine by Implementing IViewEngine interface or by inheriting VirtualPathProviderViewEngine abstract class.
public class CustomViewEngine : VirtualPathProviderViewEngine
 {
  public CustomViewEngine()
   {
     // Define the location of the View and Partial View
    this.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.html", 
    "~/Views/Shared/{0}.html" };
    this.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.html", 
    "~/Views/Shared/{0}.html" };
   }

 protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
  {
   var physicalpath = 
   controllerContext.HttpContext.Server.MapPath(partialPath);
   return new CustomView(physicalpath);
  }

 protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
  {
   var physicalpath = 
   controllerContext.HttpContext.Server.MapPath(viewPath);
   return new CustomView(physicalpath);
  }
 }

public class CustomView : IView
{
 private string _viewPhysicalPath;
 public CustomView(string ViewPhysicalPath)
  {
    _viewPhysicalPath = ViewPhysicalPath;
  }

 public void Render(ViewContext viewContext, System.IO.TextWriter writer)
  {
    //Load File
    string rawcontents = File.ReadAllText(_viewPhysicalPath);
    //Perform Replacements
    string parsedcontents = Parse(rawcontents, viewContext.ViewData);
    writer.Write(parsedcontents);
  }

public string Parse(string contents, ViewDataDictionary viewdata)
 {
   return Regex.Replace(contents, "\\{(.+)\\}", m => GetMatch(m, viewdata));
 }

public virtual string GetMatch(Match m, ViewDataDictionary viewdata)
 {
  if (m.Success)
   {
     string key = m.Result("$1");
     if (viewdata.ContainsKey(key))
      {
        return viewdata[key].ToString();
      }
   }
   return string.Empty;
 }
}


29. How to register Custom View Engine in ASP.NET MVC?

To use your custom View Engine, you need to register it by using global.asax.cs file Application_Start() method, so that the framework will use your custom View Engine instead of the default one.
protected void Application_Start()
{
 //Register Custom View Engine
  ViewEngines.Engines.Add(new CustomViewEngine());
 //other code is removed for clarity
}

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

Yes, you can remove default view engines (Razor and WebForm) provided by ASP.NET MVC.
protected void Application_Start()
{
  //Remove All View Engine including Webform and Razor
  ViewEngines.Engines.Clear();
}
0 Komentar untuk "Part - 6 : ASP.NET MVC Interview Question's Answer"

Back To Top