您的位置:首页 > 编程语言 > ASP

asp .net 模板引擎 使用 Razor 生成html静态页面

2020-05-02 12:20 2126 查看

刚开始不是理解 写完之后 觉得还蛮简单的

分为这几个步骤

1.获取页面模板Html

2.获取数据

3.解析模板和数据,生成静态页Html代码 

4.生成静态文件

 

模板形式是mvc的模式,会mvc 看一下就懂了

 

 

主要是第2步和第3步

需要应用下面文件

RazorEngine.dll

System.Web.Razor.dll

 

 

/// <summary>
/// 获取页面的Html代码
/// </summary>
/// <param name="url">模板页面路径</param>
/// <param name="encoding">页面编码</param>
/// <returns></returns>
public string GetHtml(string url, System.Text.Encoding encoding)
{
byte[] buf = new WebClient().DownloadData(url);
if (encoding != null) return encoding.GetString(buf);
string html = System.Text.Encoding.UTF8.GetString(buf);
encoding = GetEncoding(html);
if (encoding == null || encoding == System.Text.Encoding.UTF8) return html;
return encoding.GetString(buf);
}
/// <summary>
/// 获取页面的编码
/// </summary>
/// <param name="html">Html源码</param>
/// <returns></returns>
public System.Text.Encoding GetEncoding(string html)
{
string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
string charset = Regex.Match(html, pattern).Groups["charset"].Value;
try { return System.Text.Encoding.GetEncoding(charset); }
catch (ArgumentException) { return null; }
}

/// <summary>
/// 创建静态文件
/// </summary>
/// <param name="result">Html代码</param>
/// <param name="createpath">生成路径</param>
/// <returns></returns>
public bool CreateFileHtmlByTemp(string result, string createpath)
{


if (!string.IsNullOrEmpty(result))
{


//if (string.IsNullOrEmpty(createpath))
//{
// createpath = "/default.html";
//}
//string filepath = createpath.Substring(createpath.LastIndexOf(@"\"));
//createpath = createpath.Substring(0, createpath.LastIndexOf(@"\"));
//if (!Directory.Exists(createpath))
//{
// Directory.CreateDirectory(createpath);
//}
//createpath = createpath + filepath;

try
{
FileStream fs2 = new FileStream(createpath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs2, new System.Text.UTF8Encoding(false));//去除UTF-8 BOM
sw.Write(result);
sw.Close();
fs2.Close();
fs2.Dispose();
return true;
}
catch { return false; }
}
return false;
}

 

主要部分

1.数据

var GetJosn = new DataName() { Name = "这里是文章标题", ThemeName = "<span style=\"color:red;\">这里是文章内容</span>" };

var Entity = new DataName();//实体


这一部分要注意的是实体类

反射部分

Type typeT = GetJosn.GetType();
Type typeEn = Entity.GetType();

 

System.Reflection.PropertyInfo[] propertyinfosT = typeT.GetProperties();
foreach (System.Reflection.PropertyInfo propertyinfoT in propertyinfosT)
{
System.Reflection.PropertyInfo propertyinfoEn = typeEn.GetProperty(propertyinfoT.Name);
if (propertyinfoEn != null && propertyinfoT.GetValue(GetJosn, null) != null)
{
propertyinfoEn.SetValue(Entity, propertyinfoT.GetValue(GetJosn, null), null);
}
}

 

 

html 页面是代码

<body style="color:aqua;font-size:30px;">
@Model.Name
@Model.ThemeName
</body>

 


//解析模板生成静态页Html代码
result = Razor.Parse(“模板hmtl”, Entity);//Entity尸体数据

 看得懂 主要部分基本就可以了

 

补充一个东西

我用的是.net 4.0  2个bll文件

RazorEngine.dll是33k

System.Web.Razor.dll是175k

其他的 可能不能使用注意

http://razorengine.codeplex.com/ 可以在这里下

 

转载于:https://www.cnblogs.com/Geok/p/7063916.html

banglianmiao0070 原创文章 0获赞 0访问量 535 关注 私信
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: