| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 465 人关注过本帖
标题:。net动态生成静态实例
只看楼主 加入收藏
work001
Rank: 3Rank: 3
等 级:禁止访问
帖 子:76
专家分:114
注 册:2010-4-9
收藏
 问题点数:0 回复次数:0 
。net动态生成静态实例
动态生成静态页思路:
1:建立一个html页面模板,在这个页面中把你想要动态显示的地方用特殊的字符串表示(如$htmlstrstr$);
2:在程序中用将这个html页面读到一个字符串变量如str;
3:用字符串的resplace方法将在第一步中特殊字符替换成你想要的内容;
4保存;
return null;
207            }
208        }
209        /**//// <summary>
210        /// 开始进行aspxTohtml转换
211        /// </summary>
212        /// <param name="htmlparam">html模板页中的所有参数数组</param>
213        /// <param name="aspxparam">aspx页面中要代替html模板页中参数值数组</param>
214        /// <returns>返回值为成功创建后的文件名称</returns>
215        public string StartConvert(string[] htmlparam,string[] aspxparam)
216        {
217            //先调用setTemplateFileparameter 和setAspxFileparameter方法,进行付值操作
218            setTemplateFileparameter(htmlparam);
219            setAspxFileparameter(aspxparam);
220            //
221            string fn=this.StartConvert();
222            //
223            _convertedFilename=fn;
224            //
225            return fn;
226        }
227        
228        /**//// <summary>
229        /// 用时间加随机数生成一个文件名
230        /// </summary>
231        /// <returns></returns>
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))+".html";
245            //return DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
246        }
247        /**//// <summary>
248        /// 进行转换处理
249        /// </summary>
250        /// <returns>返回以时间命名的文件名</returns>
251        private string writeFile()
252        {
253            
254            // 读取模板文件
255            string temp = HttpContext.Current.Server.MapPath(this.TemplateFilePath);
256            StreamReader sr=null;            
257            string str="";
258            try
259            {
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            finally
270            {
271                sr.Close();
272            }            
273            // 替换内容
274            // 这时,模板文件已经读入到名称为str的变量中了
275            for(int i=0;i<this.TemplateParamCount;i++)
276            {
277                str =str.Replace(this._templateFileparameter[i],this._aspxFileparameter[i]);
278            }        
279
280            return savefile(str);
281        }
282
283        /**//// <summary>
284        ///
285        /// </summary>
286        /// <param name="str"></param>
287        /// <returns></returns>
288
289        private string savefile(string str)
290        {
291            // 写文件
292            StreamWriter sw=null;
293            try
294            {
295               
296                string path = HttpContext.Current.Server.MapPath(this.HtmlFilePath);
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            finally
309            {
310                sw.Close();
311            }
312            return "";
313        }
314
315        /**//// <summary>
316        /// 传入URL返回网页的html代码
317        /// </summary>
318        /// <param name="Url">URL</param>
319        /// <returns></returns>
320        public string getUrltoHtml(string Url)
321        {            
322            try
323            {
324                wReq = (Url);            
325                wResp =wReq.GetResponse();               
326                respStream  = wResp.GetResponseStream();               
327                reader = new (respStream, System.Text.Encoding.GetEncoding("gb2312"));
328                return  savefile(reader.ReadToEnd());
329
330            }
331            catch(System.Exception ex)
332            {
333                WriteErrFile(ex);
334            }
335            return "";
336        }
337        #endregion
338
339
340        构造#region 构造        
341        
342        public AspxToHtml()
343        {
344            //
345            // TODO: 在此处添加构造函数逻辑
346            //            
347        }
348
349        private void settemplateParamCount(int templateParamCount)
350        {
351            if (templateParamCount>0)
352                this.TemplateParamCount=templateParamCount;
353        }
354        /**//// <summary>
355        /// 提供欲代替的参数个数
356        /// </summary>
357        /// <param name="templateParamCount"></param>
358        public AspxToHtml(int templateParamCount)
359        {   
360            settemplateParamCount(templateParamCount);
361            
362        }
363        /**//// <summary>
364        ///
365        /// </summary>
366        /// <param name="templateParamCount">html模板页中的参数个数</param>
367        /// <param name="htmlFilePath">生成的html文件所存放的文件夹路径</param>
368        /// <param name="templateFilePath">html模板页路径</param>
369        public AspxToHtml(int templateParamCount,string htmlFilePath,string templateFilePath)
370        {
371            settemplateParamCount(templateParamCount);
372            this.HtmlFilePath        =    htmlFilePath;
373            this.TemplateFilePath    =    templateFilePath;
374            
375        }
376        #endregion
377
378        #region
379        
380        /**//// <summary>
381        /// 把错误写入文件方法#region 把错误写入文件方法
382        /// </summary>
383        /// <param name="ee"></param>
384        private  void WriteErrFile(Exception ee)
385        {
386            
387            FileStream fs1 = new FileStream(HttpContext.Current.Server.MapPath(ErrLogPath), );
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        #endregion
398    }
399}
400


搜索更多相关主题的帖子: 实例 动态 静态 
2010-04-09 10:09
快速回复:。net动态生成静态实例
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016802 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved