1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > asp .net 模板引擎 使用 Razor 生成html静态页面

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

时间:2019-11-14 18:18:22

相关推荐

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

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

分为这几个步骤

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

其他的 可能不能使用注意

/ 可以在这里下

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。