Brief introduction of Razor template engine

  • 2021-10-11 18:06:01
  • OfStack

1. Tools download:

Assembly Download Address: Link: http://pan. baidu. com/s/1hsGJV5y Password: pfja

For visual studio2015 Assembly Download Address: Link: http://pan.baidu.com/s/1nvn31IL Password: 4v7h

If the error "File not Found Exception Cannot Load System. Web. Razor. dll Version = 2.0. 0" occurs while using VS 2015 and later, delete the old RazorEginee. dll with the assembly in the attachment below, and then add RazorEngine. NET 4.0. dll and System. Web. Razor. Net 4.0. dll to the project reference.

2. Core code encapsulation:


public static string ParseRazor(HttpContext context,
   string csHtmlVirtualPath, object model = null)
  {
   string fullpath = context.Server.MapPath(csHtmlVirtualPath);// Convert virtual path to absolute path 
   string cshtml = File.ReadAllText(fullpath); // Will the whole cshtml The code is read out 
   string cacheName = fullpath + File.GetLastWriteTime(fullpath);// Returns the date and time when the specified file or directory was last written. 
   string html = Razor.Parse(cshtml, model, cacheName);
   return html;
  }
  /// <summary>
  ///  Directly will read the cshtml After the code is converted, it is directly returned to the client 
  /// </summary>
  /// <param name="context"></param>
  /// <param name="csHtmlVirtualPath"></param>
  /// <param name="model"></param>
  public static void OutputRazor(HttpContext context,
   string csHtmlVirtualPath, object model = null)
  {
   string html = ParseRazor(context, csHtmlVirtualPath, model);
   context.Response.Write(html);    
  }  

Related articles: