• ASP.NET生成静态页面方法(三种)


    第一种方法:向服务器的动态页面发送请求,获取页面的html代码。这种方法缺点显而易见:速度慢。另外如果请求的动态页面有验证控件的话,返回的html页面却无法进行数据验证。但这种方法写起来比较简单。主要代码如下:

    #region//生成被请求URL静态页面
    public static void getUrltoHtml(string Url,string Path)//Url为动态页面地址,Path为生成的静态页面
    {
    try
    {
       System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
         // Get the response instance.
       System.Net.WebResponse wResp =wReq.GetResponse();
         // Get the response stream.
       System.IO.Stream respStream = wResp.GetResponseStream();
         // Dim reader As StreamReader = New StreamReader(respStream)
       System.IO.StreamReader reader = new System.IO.StreamReader(respStream,System.Text.Encoding.GetEncoding("gb2312"));
       string str=reader.ReadToEnd();
       System.IO.StreamWriter sw=new System.IO.StreamWriter(Path,false,System.Text.Encoding.GetEncoding("gb2312"));
       sw.Write(str);
       sw.Flush();
       sw.Close();
       System.Web.HttpContext.Current.Response.Write("<script>alert('页面生成成功!');</script>");
    }
    catch(System.Exception ex)
    {
       System.Web.HttpContext.Current.Response.Write("<script>alert('页面生成失败!"+ex.Message+"');</script>");
    }
    }
    #endregion

    第二种方法:从文件读取模版,替换模版中的参数后输出文件,这种方法的生成速度上比第一种要快许多,而且模版内容可以用工具任意编辑
    主要代码:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Text;

    namespace xinxi
    {
    /// <summary>
    /// CreatePage的摘要说明。
    /// </summary>
    // www.365xinxi.net
    // 此类是生成静态网页的小程序

    public class Create
    {
       public void CreatePage()
       {

       }
       public static bool WriteFile(string strText,string strContent,string strAuthor)
       {
        string path = HttpContext.Current.Server.MapPath("/test/");//文件输出目录
        Encoding code = Encoding.GetEncoding("gb2312");
        // 读取模板文件
        string temp = HttpContext.Current.Server.MapPath("/template/test.html");//模版文件
        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();
        }


        string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";//静态文件名

        // 替换内容
        // 这时,模板文件已经读入到名称为str的变量中了
        str = str.Replace("ShowArticle",strText); //模板页中的ShowArticle
        str = str.Replace("biaoti",strText);
        str = str.Replace("content",strContent);
        str = str.Replace("author",strAuthor);
        // 写文件
        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;
       }
    }
    }
    //原理是利用System.IO中的类读写模板文件,然后用Replace替换掉模板中的标签,写入静态html

    第三种方法:如果生成的文件数量比较多,第二种方法就要反复读取模版内容,这时可以用第三种方法——直接将你的模版写在代码中,和上次我写的网站Header和Footer的制作方法类似:
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.OleDb;
    using System.Text;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace xinxi
    {
    /// <summary>
    /// 自定义公共函数
    /// </summary>
    public class myfun
    {

       #region//定义模版页
       public static string SiteTemplate()
       {
        string str="";
        str+="...";//模版页html代码
        return str;
       }

       #endregion

       public static bool WriteFile(string strText,string strContent,string strAuthor)
       {
        string path = HttpContext.Current.Server.MapPath("/test/");//文件输出目录
        Encoding code = Encoding.GetEncoding("gb2312");

        StreamWriter sw=null;
        string str=SiteTemplate();//读取模版页面html代码

        string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";//静态文件名

        // 替换内容
        str = str.Replace("ShowArticle",strText);
        str = str.Replace("biaoti",strText);
        str = str.Replace("content",strContent);
        str = str.Replace("author",strAuthor);
        // 写文件
        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;
       }


    }
    }

    三种方法比较起来生成速度由慢到快,易操作性则由简到繁。还请根据实际情况选择合适的方法。

  • 相关阅读:
    Intellij Idea安装主题包
    Spring中bean的含义
    IntelliJ IDEA 中 右键运行时没有run;新建时,选项没有Java class的解决方法和具体解释
    Linux下修改profile后用户无法登陆的问题
    11 | 怎么给字符串字段加索引?
    09 | 普通索引和唯一索引,应该怎么选择?
    08 | 事务到底是隔离的还是不隔离的?
    07 | 行锁功过:怎么减少行锁对性能的影响?
    06 | 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
    05 | 深入浅出索引(下)
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175919.html
Copyright © 2020-2023  润新知