zoukankan      html  css  js  c++  java
  • 三种C#.net生成静态页面的方法

    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(" ");
     
    }
     
    catch(System.Exception ex)
     
    {
     
       System.Web.HttpContext.Current.Response.Write(" ");
     
    }
     
    }
     
    #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
     
    {
     
    ///
     
    /// CreatePage的摘要说明。
     
    ///
     
    // 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
     
    {
     
    ///
     
    /// 自定义公共函数
     
    ///
     
    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;
     
       }
     
    }
     
    }
  • 相关阅读:
    浏览器渲染HTML页面步骤
    JavaScript中必记英语单词及含义
    JavaScript中的线程与进程
    成绩转换 题解
    计算球的体积 题解 #define
    计算两点间的距离 题解
    ASCII码排序 题解
    python学习——协程
    python学习——进程
    python学习——锁
  • 原文地址:https://www.cnblogs.com/heiyeqishi/p/3648715.html
Copyright © 2011-2022 走看看