服务器之家

服务器之家 > 正文

Razor模板引擎简单介绍

时间:2020-05-14 12:24     来源/作者:风雪幻林

一.工具下载:

程序集下载地址:链接: http://pan.baidu.com/s/1hsGJV5y 密码: pfja

适用于visual studio2015程序集下载地址:链接: http://pan.baidu.com/s/1nvn31IL 密码: 4v7h

如果在使用VS2015等更高版本的时候出现“File not Found Exception Cannot Load System.Web.Razor.dll Version=2.0.0”这样的错误,请换用下面附件中的程序集把旧的RazorEginee.dll删掉,然后把 RazorEngine.NET4.0.dll和System.Web.Razor.Net4.0.dll添加到项目引用中。 

二.核心代码代码封装:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static string ParseRazor(HttpContext context,
   string csHtmlVirtualPath, object model = null)
  {
   string fullpath = context.Server.MapPath(csHtmlVirtualPath);//将虚拟路径转换为绝对路径
   string cshtml = File.ReadAllText(fullpath); //将整个cshtml代码读取出来
   string cacheName = fullpath + File.GetLastWriteTime(fullpath);//返回上次写入指定文件或目录的日期和时间。
   string html = Razor.Parse(cshtml, model, cacheName);
   return html;
  }
  /// <summary>
  /// 直接将读取的cshtml代码转换完后,直接返回给客户端
  /// </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);   
  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/fengxuehuanlin/p/5313354.html

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部