两种方法
方法一:
/**/
/// <summary>
/// 静态生成页面的方法
/// </summary>
/// <param name="strPageUrl">生成源</param>
/// <param name="strFileName">生成到</param>
private
bool
MakePage(
string
strPageUrl,
string
strFileName)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string strDir,strFilePage;
strDir = @"/htm/MakePage/";//更新到的文件夹
strFilePage = Server.MapPath(strDir+strFileName);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
StreamWriter sw=null;
//获得aspx的静态html
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Directory.Exists(Server.MapPath(strDir)))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.CreateDirectory(Server.MapPath(strDir));
}
if(File.Exists(strFilePage))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
File.Delete(strFilePage);
}
sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("GB2312"));
Server.Execute(strPageUrl,sw);
}
catch(Exception ex)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
msg.ShowMsg("'"+strFileName+"'生成出错:"+ex.Message);
return false;//生成到出错
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sw.Flush();
sw.Close();
sw = null;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return true;
}
方法二:
/**/
/// <summary>
/// 静态生成页面的方法
/// </summary>
/// <param name="strPageUrl">生成源</param>
/// <param name="strFileName">生成到</param>
private
bool
MakePage(
string
strPageUrl,
string
strFileName)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string strDir,strFilePage;
strDir = @"/htm/MakePage/";//更新到的文件夹
strFilePage = Server.MapPath(strDir+strFileName);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
StreamWriter sw=null;
//获得aspx的静态html
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Directory.Exists(Server.MapPath(strDir)))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.CreateDirectory(Server.MapPath(strDir));
}
if(File.Exists(strFilePage))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
File.Delete(strFilePage);
}
sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("GB2312"));
System.Net.WebRequest wReq = System.Net.WebRequest.Create(strPageUrl);
System.Net.WebResponse wResp = wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
sw.Write( reader.ReadToEnd());
}
catch(Exception ex)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
msg.ShowMsg("'"+strFileName+"'生成出错:"+ex.Message);
return false;//生成到出错
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sw.Flush();
sw.Close();
sw = null;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return true;
}
总结:方法一只可用用虚拟路径(如:不能用
http://www.qq.com),而方法二相反。
转载于:https://www.cnblogs.com/QiuYun/archive/2007/01/16/621836.html