_cuclife.com
当前位置:cuclife.com > IT > ASP.NET >

三种ASP.NET生成静态页面方法比较

第一种方法:向服务器的动态页面发送请求,获取页面的html代码。这种方法缺点显而易见:速度慢。另外如果请求的动态页面有验证控件的话,返回的html页面却无法进行数据验证。但这种方法写起来比较简单。主要代码如下:
01.#region//生成被请求URL静态页面
02.public static void getUrltoHtml(string Url,string Path)//Url为动态页面地址,Path为生成的静态页面
03.{
04. try
05. {
06. System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
07. // Get the response instance.
08. System.Net.WebResponse wResp =wReq.GetResponse();
09. // Get the response stream.
10. System.IO.Stream respStream = wResp.GetResponseStream();
11. // Dim reader As StreamReader = New StreamReader(respStream)
12. System.IO.StreamReader reader = new System.IO.StreamReader(respStream,System.Text.Encoding.GetEncoding("gb2312"));
13. string str=reader.ReadToEnd();
14. System.IO.StreamWriter sw=new System.IO.StreamWriter(Path,false,System.Text.Encoding.GetEncoding("gb2312"));
15. sw.Write(str);
16. sw.Flush();
17. sw.Close();
18. System.Web.HttpContext.Current.Response.Write("<mce:script type="text/javascript"><!--
19.alert('页面生成成功!');
20.// --></mce:script>");
21. }
22. catch(System.Exception ex)
23. {
24. System.Web.HttpContext.Current.Response.Write("<mce:script type="text/javascript"><!--
25.alert('页面生成失败!"+ex.Message+"');
26.// --></mce:script>");
27. }
28.}
29.#endregion
#region//生成被请求URL静态页面
public static void getUrltoHtml(string Url,string Path)//Url为动态页面地址,Path为生成的静态页面
{
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
// Get the response instance.
System.Net.WebResponse wResp =wReq.GetResponse();
// Get the response stream.
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)
System.IO.StreamReader reader = new System.IO.StreamReader(respStream,System.Text.Encoding.GetEncoding("gb2312"));
string str=reader.ReadToEnd();
System.IO.StreamWriter sw=new System.IO.StreamWriter(Path,false,System.Text.Encoding.GetEncoding("gb2312"));
sw.Write(str);
sw.Flush();
sw.Close();
System.Web.HttpContext.Current.Response.Write("<mce:script type="text/javascript"><!--
alert('页面生成成功!');
// --></mce:script>");
}
catch(System.Exception ex)
{
System.Web.HttpContext.Current.Response.Write("<mce:script type="text/javascript"><!--
alert('页面生成失败!"+ex.Message+"');
// --></mce:script>");
}
}
#endregion

文章来源:网络整理  本站编辑:兰特
上一篇:ASP.NET邮件按时间发送
下一篇:F#教程: 无参函数和for语句
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)