在非MVC下RazorEngine的使用

1、通过nuget安装RazorEngine

Razor引擎下载地址:http://github.com/Antaris/RazorEngine

2、来看代码
后台部分:

string fullPath = AppDomain.CurrentDomain.BaseDirectory+@"/Views/MyView.cshtml";//拿到cshtml文件路径
string cshtml = File.ReadAllText(fullPath);//得到文件内容
string cache = cache + EncrypHelper.MD5(cshtml);
string myhtml = Razor.Parse(cshtml, new {name="tom"}, null, cache)

再看一下页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	 <title>测试</title>
	</head>
	<body>
	 <ul>@Model.name</ul>
	</body>
</html>

@Model.属性 可以直接使用。
也可以指定类型Razor.Parse<T>(text, model);

看到有文章说Razor.Parse每次都会将动态编译的东西保存在内存中,参考:https://stackoverflow.com/questions/12301515/razorengine-memory-usage
所以,有下面解决办法:

  1. 加上cache
  2. 不用用3.0
  3. 用Precompiling Templates方法,参考:https://razorengine.codeplex.com/wikipage?title=Quick Start Guide&referringTitle=Documentation
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值