邀朋友种豆,一起分享吧
喜欢购买正品行货?那就去品牌街

Aspx页面生成html静态页面大全

来自:种豆 时间:2017-12-29 阅读:534次 原文链接
1 模板替换法

    /// <summary>
    ///
    /// </summary>
    /// <param name="localPath">静态页面保存目录</param>
    /// <param name="modelFileName">模板文件名(含目录)</param>
    /// <param name="htmlFileName">静态Html文件名</param>
    /// <param name="encoding">文件编码格式</param>
    /// <param name="strTitle">替换参数内容1</param>
    /// <param name="strContent">替换参数内容2</param>
    /// <returns></returns>
    public bool WriteFile(string localPath, string modelFileName, string htmlFileName, string encoding, string strTitle, string strContent)
    {
        string path = HttpContext.Current.Server.MapPath(localPath);
        Encoding code = Encoding.GetEncoding(encoding);
        // 读取模板文件
        string temp = HttpContext.Current.Server.MapPath(modelFileName);
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd(); // 读取文件
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }         // 替换内容
        //这时,模板文件已经读入到名称为str的变量中了
        str = str.Replace("biaoti", strTitle);
        str = str.Replace("content", strContent);
        // 写文件
        try
        {
            sw = new StreamWriter(path + "\\" +htmlFileName, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
        return true;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string localPath = "/new/";
        string modelFileName = "/models/myModel.html";
        string htmlFileName = "newWeb.html";
        string encoding = "GB2312";
        string strTitle = "替换后的标题";
        string strContent = "替换后的内容";         if (WriteFile(localPath, modelFileName, htmlFileName, encoding, strTitle, strContent))
        {
            Response.Write("添加成功");
        }
        else
        {
            Response.Write("生成HTML出错!");
        }
    }
模板页myModel.html代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
 <title>biaoti</title>
</HEAD>
 <body>
content
</body>
</HTML>

2 使用WebClient将Aspx生成为静态html页面

    /// <summary>
    ///
    /// </summary>
    /// <param name="localpath">静态页保存目录</param>
    /// <param name="webpath">aspx页面URL(如:http://www.sowsoy.com/xxxx.aspx)</param>
    /// <param name="setPath">静态文件名</param>
    /// <param name="fFormat">文件编码格式</param>
    /// <returns></returns>
    public string GetNetPageInfo(string localpath,string webpath,string setPath ,string fFormat)
     {
         try
         {
              WebClient myWebClient = new WebClient();
              myWebClient.Credentials = CredentialCache.DefaultCredentials;
              byte[] pageData = myWebClient.DownloadData(webpath);
              string pageHtml = Encoding.GetEncoding(fFormat).GetString(pageData);
              using (StreamWriter sw = new StreamWriter(localpath + setPath, false, Encoding.GetEncoding("gb2312")))//将获取的内容写入文本
              {
                  sw.Write(pageHtml);
                  sw.Dispose();
              }               return "生成成功了! " + localpath + setPath;
         }
         catch (WebException ex)
         {
              return ex.Message;
         }
     }
    protected StringBuilder str = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)     {
        str.Append((GetNetPageInfo(Server.MapPath(""), "http://www.sowsoy.com", "\\myweb.html", "gb2312")));
        str.Append("<br>");
    }
3 XML解析器(由模板文件直接生成静态Html文件)
    模板文件是和程序分离的,模板可以做的很复杂,你改页面不用去动程序。

数据源文件 a.xml

<root>
    <a><![CDATA[AAAAAA <br /> BBBBBB<br />CCCCCC]]></a>
</root>
模板文件 a.xslt

  <xsl:stylesheet   version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  <xsl:template   match="/">  
  <html>  
    <body><xsl:value-of disable-output-escaping="yes" select="/root/a"/></body>  
  </html>  
  </xsl:template>  
  </xsl:stylesheet>
转换代码(模板文件中有一行 select='/root/a',代表xml文件中根下的a节点内容就要在这里显示)

    private void xml2html(string xml_file, string xslt_file, string html_file)
    {
        try
        {
            XslTransform xslt = new XslTransform();
            XmlDocument xml = new XmlDocument();
            xslt.Load(xslt_file);
            xml.Load(xml_file);             FileStream writer = new FileStream(html_file, FileMode.Create, FileAccess.ReadWrite);
            xslt.Transform(xml, null, writer, null);
            writer.Flush();
            writer.Close();
        }
        catch (Exception ex)
        {
            throw new Exception("生成news.163.com/auto车型信息静态页面失败:" + ex.Message); ;
        }
    }
 
关于种豆 ┊ 联系我们 ┊ 免责声明 ┊ 发帖须知 ┊ 请提意见 ┊ 站点地图
本站为个人爱好兴趣分享网站,不代表本人观点,如有侵权请联系QQ3033380280进行处理
sowsoy.com 版权所有 Copyright©2010-2021 备案号:蜀ICP备2020025376号-3
Email:sowsoy#hotmail.com