分享一下c#RazorEngine的源码,做动态模版和生成静态化的好办法
源码如下
使用方法也很简单
baseModel
[C#] 纯文本查看 复制代码 /// <summary>
/// 基础Model
/// </summary>
public class BaseModel
{
private int Siteid = 0;
/// <summary>
/// 基础Model的重构方法,需要网站ID
/// </summary>
/// <param name="_siteid"></param>
public BaseModel(int _siteid)
{
Siteid = _siteid;
}
/// <summary>
/// 获取对应的Title
/// </summary>
/// <returns></returns>
public string GetTitle()
{
return Siteid.ToString() + "中国建站第一品牌,十分钟满足您所有的需求";
}
public List<string> getList(int n)
{
List<string> list = new List<string>();
for (int i = 0; i < n; i++)
{
list.Add(i.ToString() + "a1dDLDFDFfdFFFFs22");
}
return list;
}
}
模版
[C#] 纯文本查看 复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=7" />
<meta name="Copyright" content="ruituoyun.com" />
<meta name="apple-itunes-app" content="app-id=546639559" />
<title>@Model.GetTitle()</title>
<meta name="keywords" content="" />
<meta name="description" content="中国建站第一品牌,,安全,稳定,快速,成本低,服务好!" />
</head>
<body>
@foreach (var item in Model.getList(12))
{
<br /><br /> <div> @item</div><br />
<div>下面是小写<br/> @item.ToLower()</div>
}
</body>
</html>
测试代码
[C#] 纯文本查看 复制代码 //模板
string cshtml = string.Empty;
//处理后的aspx页岩面
string straspx = string.Empty;
//读取文件
using (StreamReader sr = new StreamReader(@"D:\text.cshtml"))
{
cshtml = sr.ReadToEnd();
}
//生成aspx页面内容
straspx = RazorBuilder.CshtmlToAspx(cshtml, EnumBaseModel.BaseModel);
Response.Clear();
Response.Write(straspx);
Response.End();
效果
源码下载
RazorEngine.zip
(183.04 KB, 下载次数: 7)
|