首页 > 编程 > .NET > 正文

Asp.net生成htm静态文件的两种途径

2024-07-10 13:08:28
字体:
来源:转载
供稿:网友


//51aspx.com生成静态页演示文件,转载请保留该信息
  2    public partial class _default : system.web.ui.page
  3    ...{
  4        protected void page_load(object sender, eventargs e)
  5        ...{
  6          
  7        }
  8
  9        //根据模板生成,保持在html文件夹中(部分源码搜集于网络)
 10        protected void button1_click(object sender, eventargs e)
 11        ...{
 12            //源码是替换掉模板中的特征字符
 13
 14            string mbpath =server.mappath("template.htm");
 15            encoding code = encoding.getencoding("gb2312");
 16            streamreader sr = null;
 17            streamwriter sw = null;
 18            string str = null;
 19
 20            //读取
 21            try
 22            ...{
 23                sr = new streamreader(mbpath, code);
 24                str = sr.readtoend();
 25
 26            }
 27            catch (exception ex)
 28            ...{
 29                throw ex;
 30            }
 31            finally
 32            ...{
 33                sr.close();
 34            }
 35
 36            //根据时间自动重命名,扩展名也可以自行修改
 37            string filename = datetime.now.tostring("yyyymmddhhmmss") + ".htm";
 38            str = str.replace("$title$", txttitle.text);//替换title
 39            str = str.replace("$content$", txtcontent.text);//替换content
 40
 41            //生成静态文件
 42            try
 43            ...{
 44                sw = new streamwriter(server.mappath("htm/") + filename, false, code);
 45                sw.write(str);
 46                sw.flush();
 47
 48            }
 49            catch (exception ex)
 50            ...{
 51                throw ex;
 52            }
 53            finally
 54            ...{
 55                sw.close();
 56                response.write("恭喜<a href=htm/"+filename+" target=_blank>"+filename+"</a>已经生成,保存在htm文件夹下!");
 57            }
 58
 59
 60        }
 61
 62
 63        //根据url地址生成静态页保持
 64        protected void button2_click(object sender, eventargs e)
 65        ...{
 66            encoding code = encoding.getencoding("utf-8");
 67            streamreader sr = null;
 68            streamwriter sw = null;
 69            string str = null;
 70
 71            //读取远程路径
 72            webrequest temp = webrequest.create(txturl.text.trim());
 73            webresponse mytemp = temp.getresponse();
 74            sr = new streamreader(mytemp.getresponsestream(), code);
 75            //读取
 76            try
 77            ...{
 78                sr = new streamreader(mytemp.getresponsestream(), code);
 79                str = sr.readtoend();
 80
 81            }
 82            catch (exception ex)
 83            ...{
 84                throw ex;
 85            }
 86            finally
 87            ...{
 88                sr.close();
 89            }
 90            string filename = datetime.now.tostring("yyyymmddhhmmss") + ".htm";
 91
 92            //写入
 93            try
 94            ...{
 95                sw = new streamwriter(server.mappath("htm/") + filename, false, code);
 96                sw.write(str);
 97                sw.flush();
 98
 99            }
100            catch (exception ex)
101            ...{
102                throw ex;
103            }
104            finally
105            ...{
106                sw.close();
107                response.write("恭喜<a href=htm/" + filename + " target=_blank>" + filename + "</a>已经生成,保存在htm文件夹下!");
108            }
109
110        }
111    }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表