html模板转换aspx,根据html页面模板动态生成html页面(c#类)

一直以为动态生成静态页面不好做,昨天在网上找了下,我晕,其实很基本

,思路大概是这样的,1:建立一个html页面模板,在这个页面中把你想要动态显示的地点

用特殊的字符串示意

(如$htmlstrstr$);2:在程序中用将这个html页面读到一个字符串变量如str;3:用字符串的resplace要领

将在第一步中特殊字符替换成你想要的内容;4保存;OK,so easy,今天就用C#写了一个这样的类,用来处理动态生成html页面的,自认为还写的完整,刚接触.NET不久,望指教,完整代码与示例在此下载:下载/ArtImage/20070514/341591-1.rar

转贴请注明出处,谢谢!注:此类中的代码不全是原创,部份代码参照网友的代码!

以下是转换类的代码

代码  1using System;  2using System.Text;  3using System.Web;  4using System.Configuration;  5using System.IO;  6namespace solucky  7{  8    /**   9    /// AspxToHtml 的摘要说明。 10    /// 注:运用

此类,你可以在web.config文件对模板类实行

配置.如下 11    /**//* 12     13     14         15    */ 16    /**  17    public class AspxToHtml 18    { 19        /**  20        /// 模板文件中要替代的参数个数 21        ///  22        private int            _templateParamCount=0; 23        /**  24        /// 模板文件所在的路径 25        ///  26        private string        _templateFilePath        =ConfigurationSettings.AppSettings["templateFilePath"]; 27        /**  28        /// 转换后的html文件所存放的路径 29        ///  30        private string        _htmlFilePath            =ConfigurationSettings.AppSettings["htmlFilePath"]; 31         32        /**  33        /// 模板页页面编码 34        ///  35        private Encoding _templateHtmlCode            =Encoding.GetEncoding("gb2312"); 36 37        /**  38        /// 转换后的文件编码 39        ///  40        private Encoding _code = Encoding.GetEncoding("gb2312"); 41 42        /**  43        /// 转换后的html文件名 44        ///  45        private string  _convertedFilename=""; 46        /**  47        /// 模板文件中的参数 48        ///  49        private string[]    _templateFileparameter    ; 50         51        /**  52        /// aspx文件中的要代替HTML文件中的参数实际值 53        ///  54        private string[]    _aspxFileparameter; 55 56        private string _errlogPath = ConfigurationSettings.AppSettings["ErrLogPath"]; 57 58        属性#region 属性 59         60        /**  61        /// 模板文件中要替代的参数个数 62        ///  63        public int TemplateParamCount 64        { 65            get 66            { 67                return    this._templateParamCount; 68            } 69            set//分配参数个数时,同时为模板文件中的参数和aspx文件中的要代替HTML文件中的参数实际值这两个分配实际数组 70            { 71                if (value < 0)  72                    throw new ArgumentException(); 73 74                if(value>0)                 75                { 76                    this._templateParamCount=value; 77                    //模板文件中的参数                     78                    _templateFileparameter    = new string[value]; 79                    //aspx文件中的要代替HTML文件中的参数实际值 80                    _aspxFileparameter        = new string[value]; 81                } 82                else 83                    this._templateParamCount=0; 84            } 85        } 86         87        /**  88        /// 模板文件所在的路径 89        ///  90        ///  91        public string TemplateFilePath 92        { 93            get{    return this._templateFilePath;} 94            set{    this._templateFilePath=value;} 95        } 96        /**  97        /// 转换后的html文件所存放的路径 98        ///  99        public string HtmlFilePath100        {101            get{    return this._htmlFilePath;}102            set{    this._htmlFilePath=value;}103        }104105        /** 106        /// html模板文件编码107        /// 108        public Encoding TemplateHtmlCode109        {110            get{    return this._templateHtmlCode;}111            set{    this._templateHtmlCode=Encoding.GetEncoding(value.ToString());}112        }113        /** 114        /// 编码115        /// 116        public Encoding Code117        {118            get{    return this._code;}119            set{    this._code=Encoding.GetEncoding(value.ToString());}120        }121        /** 122        /// 不正确

文件所在路径123        /// 124        public string ErrLogPath125        {126            get{127                if(!(this._errlogPath==null))128                    return this._errlogPath;129                else130                    return "aspxTohtml_log.txt";131            }132            set{this._errlogPath=value;}133        }134135        136        #endregion137        138        操作#region 操作139140        /** 141        /// 获取转换后的html文件所在相对文件路径142        /// 如:假如

HtmlFilePath="/news/"143        /// 转换后的html文件名为200505050505.htm144        /// 则返回的值为/news/200505050505.htm145        /// 146        /// 假如

在未调用StartConvert要领

之前调用此属性则返回null147        public string HtmlFileVirtualPath148        {149            get150            {    151                if(!(this._convertedFilename==""))152                    return    this.htmFilePath+this._convertedFilename;153                else154                    return null;155            }156        }157158        /** 159        /// 为HTML页面参数数组付值160        /// 161        /// 162        public void    setTemplateFileparameter(string[] param)163        {164            try165            {166                if(param.Length==this.TemplateParamCount)167                    this._templateFileparameter=param;168                //else//与原定义的个数不等169                    //170            }171            catch(System.Exception    ex)172            {173                WriteErrFile(ex);174            }175        }176        /** 177        /// 为aspx文件中将要替换html文件中的参数数组付值178        /// 179        /// 180        public void setAspxFileparameter(string[] param)181        {182            try183            {184                if(param.Length==this.TemplateParamCount)185                    this._aspxFileparameter=param;186                //else//与原定义的个数不等187                //188            }189            catch(System.Exception    ex)190            {191            WriteErrFile(ex);192            }193        }194        /** 195        /// 开始实行

aspxTohtml转换196        /// 197        /// 返回值为成功建立

后的文件名称198        /// 在调用此要领

之前必需确定已调用setTemplateFileparameter 和setAspxFileparameter要领

实行

相应的付值操作199        public string StartConvert()200        {201            if(this._templateFileparameter.Length==this._aspxFileparameter.Length)202            {203                return writeFile();204            }205            else{206                return null;207            }208        }209        /** 210        /// 开始实行

aspxTohtml转换211        /// 212        /// html模板页中的所有参数数组213        /// aspx页面中要代替html模板页中参数值数组214        /// 返回值为成功建立

后的文件名称215        public string StartConvert(string[] htmlparam,string[] aspxp

aram)216        {217            //先调用setTemplateFileparameter 和setAspxFileparameter要领

,实行

付值操作218            setTemplateFileparameter(htmlparam);219            setAspxFileparameter(aspxp

aram);220            //221            string fn=this.StartConvert();222            //223            _convertedFilename=fn;224            //225            return fn;226        }227        228        /** 229        /// 用时间加随机数生成一个文件名230        /// 231        /// 232        private string getfilename()233        {234            //用时间加随机数生成一个文件名235            System.Threading.Thread.Sleep(50);236            string yearStr = System.DateTime.Now.Year.ToString();237            string monthStr = string.Format("{0:0#}",System.DateTime.Now.Month);238            string dayStr = string.Format("{0:0#}",System.DateTime.Now.Day); 239            string hourStr = string.Format("{0:0#}",System.DateTime.Now.Hour);240            string minuteStr = string.Format("{0:0#}",System.DateTime.Now.Minute);241            string secondStr = string.Format("{0:0#}",System.DateTime.Now.Second);242            string millisecondStr = string.Format("{0:000#}",System.DateTime.Now.Millisecond);                    243            System.Random rd = new System.Random();244            return yearStr + monthStr + dayStr + hourStr + minuteStr + secondStr + millisecondStr + string.Format("{0:0000#}",rd.Next(100))+".htm";245            //return DateTime.Now.ToString("yyyyMMddHHmmss")+".htm";246        }247        /** 248        /// 实行

转换处理249        /// 250        /// 返回以时间命名的文件名251        private string writeFile()252        {253            254            // 读取模板文件255            string temp = HttpContext.Current.Server.MapPath(this.TemplateFilePath);256            StreamReader sr=null;            257            string str=""; 258            try259            {260                sr = new StreamReader(temp, this.TemplateHtmlCode);261                str = sr.ReadToEnd(); // 读取文件262            }263            catch(Exception ex)264            {265                //HttpContext.Current.Response.Write(exp

.Message);266                //HttpContext.Current.Response.End();        267                WriteErrFile(ex);268            }269            finally270            {271                sr.Close();272            }            273            // 替换内容274            // 这时,模板文件已经读入到名称为str的变量中了275            for(int i=0;i284        /// 285        /// 286        /// 287        /// 288289        private string savefile(string str)290        {291            // 写文件292            StreamWriter sw=null;293            try294            {295                296                string path = HttpContext.Current.Server.MapPath(this.htmFilePath);297                //html文件名称    298                string htmlfilename=getfilename();299                sw = new StreamWriter(path + htmlfilename , false, this.Code);300                sw.Write(str);301                sw.Flush();302                return htmlfilename; 303            }304            catch(Exception ex)305            {                306                WriteErrFile(ex);307            }308            finally309            {310                sw.Close();311            }312            return "";313        }314315        /** 316        /// 传入URL返回网页的html代码317        /// 318        /// URL319        /// 320        public string getUrltoHtml(string Url)321        {            322            try323            {324                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);            325                System.Net.WebResponse wResp =wReq.GetResponse();                326                System.IO.Stream respStream  = wResp.GetResponseStream();                327                System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));328                return  savefile(reader.ReadToEnd());329330            }331            catch(System.Exception ex)332            {333                WriteErrFile(ex);334            }335            return "";336        }337        #endregion338339340        构造#region 构造        341        342        public AspxToHtml()343        {344            //345            // TODO: 在此处添加构造函数逻辑346            //            347        }348349        private void settemplateParamCount(int templateParamCount)350        {351            if (templateParamCount>0)352                this.TemplateParamCount=templateParamCount;353        }354        /** 355        /// 提供欲代替的参数个数356        /// 357        /// 358        public AspxToHtml(int templateParamCount)359        {    360            settemplateParamCount(templateParamCount);361            362        }363        /** 364        /// 365        /// 366        /// html模板页中的参数个数367        /// 生成的html文件所存放的文件夹路径368        /// html模板页路径369        public AspxToHtml(int templateParamCount,string htmlFilePath,string templateFilePath)370        {371            settemplateParamCount(templateParamCount);372            this.htmFilePath        =    htmlFilePath;373            this.TemplateFilePath    =    templateFilePath;374            375        }376        #endregion377378        #region379        380        /** 381        /// 把不正确

写入文件要领

#region 把不正确

写入文件要领

382        /// 383        /// 384        private  void WriteErrFile(Exception ee)385        {386            387            FileStream fs1 = new FileStream(HttpContext.Current.Server.MapPath(ErrLogPath), System.IO.FileMode.Append);388            StreamWriter sw1 = new StreamWriter(fs1);389            sw1.WriteLine("**************************************************");390            sw1.WriteLine("不正确

日期:" + System.DateTime.Now);391            sw1.WriteLine("不正确

描述:" + ee.Message);392            sw1.WriteLine("不正确

名称:" + ee.Source);393            sw1.WriteLine("细致

:" + ee.ToString());394            sw1.WriteLine("*************************************************");395            sw1.Close();396        }397        #endregion398    }399}400

http://www.cnblogs.com/solucky/archive/2006/09/07/497188.htm

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值