您现在的位置:主页 > 编程开发 > >

怎样用ASP.net生成静态html页面

时间: 2015-03-22 14:52 来源: 未知 作者: admin 收藏 百度搜索本文

asp.net,html
不用模版,直接通.net自带的类system.net

第一种:直接读取aspx成string
WebRequest mywebrq;
WebResponse mywebresp;
StreamReader sr;
Streamwriter sw;
mywebreq=WebRequest.Create(Server.Mappath("Test.aspx"));
mywebresp=mywebreq.GetResponse();
...

ASP.net生成静态html页面
 
不用模版,直接通.net自带的类system.net

第一种:直接读取aspx成string
WebRequest mywebrq;
WebResponse mywebresp;
StreamReader sr;
Streamwriter sw;
mywebreq=WebRequest.Create(Server.Mappath("Test.aspx"));
mywebresp=mywebreq.GetResponse();
sr=new streamReader(mywebresp.GetResponsestream());
strHtml=sr.ReadToEnd();
sw=File.CreateText(server.mappath("Test.htm"));
sw.writeling(strHtml);
sw.close();
Response.writeFile(Server.Mappath("Test.htm"));
结果生成的htm文件是test.aspx的复制````不行``

第二种: 读取aspx成为字节数组
byte[] byteCache=new System.Net.WebClient().DownloadData(Server.MapPath("Test.aspx"));
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("Test.htm"), FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
fs.Write(byteCache, 0, byteCache.Length);
fs.Close();
结果生成的htm文件还是test.aspx的复制````不行``

第三种:利用Server.Execute()的方法先执行成html文件到缓存再写入html
string old="test.aspx";
                         string html=Server.MapPath("test.html");
           
                         //尝试读取已有文件  
                         Stream s=GetFileStream(html);  
                         if(s!=null)//如果文件存在并且读取成功  
                         {  
                                 using(s)  
                                 {  
                                         Stream2Stream(s,Response.OutputStream);  
                                         Response.End();  
                                 }  
                         }  
       
                         //调用Main_Execute,并且获取其输出  
                         StringWriter sw = new StringWriter();  
                         Server.Execute(old,sw);
                         string content = sw.ToString();  
           
                         //输出到客户端  
                         //Response.Write(content);  

(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%