html资源工厂,c#使用简单工厂模式实现生成html文件的封装类分享

由于这段时间比较轻松,于是想到很多的企业网站,新闻网站需要将页面静态化,于是写了个封装类来实现静态文件的生成,思路比较简单,但未完善,网友可根据自己的思路将此类扩展,运用了简单工厂模式,先来看看静态类的父类:StaticBase(抽象类)

public abstract class StaticBase : IDisposable

{

///

/// 默认编码方式

///

protected Encoding code = Encoding.GetEncoding("utf-8");

///

/// 写入页面数据流

///

protected StreamWriter sw = null;

///

/// 读取页面数据流

///

protected StreamReader sr = null;

///

/// 生成的静态页面保存文件夹路径

///

protected string SavePath = "/Default/";

///

/// 模板页面的文件夹路径

///

protected string PagePath = "/Master/";

public abstract bool Osucess { set; get; }

public abstract string Errorstring { set; get; }

///

/// 具体生成静态方法

///

protected abstract bool WriteFile();

///

/// 不同模块的文件名称

///

protected Dictionary FileName

{

get

{

return new Dictionary

{

{FlagsFileName.News,"article"},

{FlagsFileName.head,"head"},

{FlagsFileName.foot,"foot"},

};

}

}

// http://www.cnblogs.com/roucheng/

#region IDisposable 成员

public void Dispose()

{

sw.Dispose();

sr.Dispose();

}

#endregion

}

#region 对应的页面名称

///

/// 对应的页面名称

///

public enum FlagsFileName : byte

{

///

/// 新闻

///

[Description("新闻")]

News = 0,

///

/// 头部

///

[Description("头部")]

head=1,

///

/// 脚部

///

[Description("脚部")]

foot=2,

}

最后的一个枚举用于定义不同位置或不同类别的静态页所对应的子类,接下来看看其中一个子类的实现(该子类是用于所有单页,如数据库中有100条新闻记录,那相应的生成100个新闻html页面,格式用模板定义的格式确定)

首先模板文件时静态的html页面,其中所有的需要从数据库中替换的字段用一对$包含,如数据库中的新闻标题字段为titles,则模板页中相应的标题位置用$titles$表示,页面如下

$Titles$

$head$

$Titles$

$Contents_tw$

 
 
 

$foot$

到这里知道个大概了吧,接下来就是这中页面类型的子类实现,我将它的名称定义为ViewPage,因为所有可以单独显示的页面都可以用这个子类,代码如下

public class ViewPage : StaticBase

{

///

/// 是否操作成功

///

private bool o_sucess = true;

///

/// 错误信息

///

private string errorstring = string.Empty;

///

/// 模板文件名称

///

private string masterhtml;

///

/// 数据源

///

private IEnumerable rowlist;

///

/// 模块类别

///

private FlagsFileName fname;

///

/// 指定命名文件的标志列(从数据库中的字段)

///

private string thekey;

public override bool Osucess

{

get { return o_sucess; }

set { o_sucess = value; }

}

public override string Errorstring

{

get { return errorstring; }

set { errorstring = value; }

}

///

/// 构造器静态生成对象

///

/// 需要生成静态文件的数据源

/// 文件类别枚举

/// 此字段为数据库表中字段,由该字段指定生成的文件名字标志

public ViewPage(DataRow[] rlist,FlagsFileName fn,string myid)

{

this.thekey = myid;

this.fname = fn;

this.rowlist = rlist;

this.masterhtml = FileName[fn] + ".html";

WriteFile();

}

protected override bool WriteFile()

{

string str = "";

try//从指定模板文件中读取html代码

{

sr = new StreamReader(HttpContext.Current.Server.MapPath(PagePath + this.masterhtml), code);

str = sr.ReadToEnd();

}

catch (Exception ex)//异常则指定返回的错误信息

{

sr.Close();

sr.Dispose();

this.o_sucess = false;

this.errorstring = ex.Message;

return this.o_sucess;

}

sr.Close();

sr.Dispose();

List fn = new List();

fn.Add(FlagsFileName.head);

fn.Add(FlagsFileName.foot);

PointPage pg = new PointPage(fn, str);

//要保存的文件名称

string htmlfilename = string.Empty;

string changestring = "";//要更改的字符串

foreach (DataRow row in this.rowlist)//遍历数据源数组中的每个数据表

{

string newString = str;

try

{

htmlfilename = FileName[fname] + "_" + row[thekey].ToString() + ".html";//给文件命名

foreach (DataColumn c in row.Table.Columns)//遍历单个数据表的列名

{

changestring = "$" + c.ColumnName + "$";

newString = newString.Replace(changestring, row[c].ToString());

}

sw = new StreamWriter(HttpContext.Current.Server.MapPath(SavePath + htmlfilename), false, code);

sw.Write(newString);

sw.Flush();

}

catch (Exception ex)

{

this.o_sucess = false;

this.errorstring = ex.Message;

return this.o_sucess;

}

}

sw.Dispose();

sw.Close();

return true;

}

}

好,到这里实现了底层的思路设计,那调用就很简单了,某个aspx页面,一个按钮button,一个点击事件Button_Click,点击事件内需要做的就是声明一个基类StaticBase,将它实例化成一个子类ViewPage,传递的参数为一个数据项集合,DataRow[]为从数据表中读取的集合,包含需要替换的字段,如select titles,contens,id from news(从新闻表中获得标识id,标题,内容),以及类型FlagsFileName.News为前天基类提到过的枚举类型,为单独页面的生成方式,已经重命名的标识列,如此处为id,则生成的页面格式为

news_1.html,news_2.html以此类推,代码如下

protected void Create_Click(object sender, EventArgs e)

{

IEnumerable rowlist = TNotice_Command.SelectTNotice(-1);

using (StaticBase sb = new ViewPage((DataRow[])rowlist, FlagsFileName.News, "NID"))

{

if (!sb.Osucess)

{

Response.Write("");

}

}

}

看到这里大家如果再从头看一遍,相信就能知道静态文件的生成的原理了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值