zoukankan      html  css  js  c++  java
  • 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)charset=(?<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/ 可以在这里下

  • 相关阅读:
    220. 存在重复元素 III
    785. 判断二分图
    欢天喜地七仙女——Beta冲刺汇总
    欢天喜地七仙女——Alpha冲刺汇总
    欢天喜地七仙女——测试随笔
    欢天喜地七仙女——beta总结
    欢天喜地七仙女——Beta冲刺十
    欢天喜地七仙女——用户调查报告
    欢天喜地七仙女——Beta冲刺九
    欢天喜地七仙女——Beta冲刺八
  • 原文地址:https://www.cnblogs.com/Geok/p/7063916.html
Copyright © 2011-2022 走看看