三种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;

   }

}

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值