ASP.NET根据URL动态生成静态页面

两种方法

方法一:

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 静态生成页面的方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strPageUrl">生成源</param>
ExpandedBlockEnd.gif        
/// <param name="strFileName">生成到</param>

None.gif          private   bool  MakePage( string  strPageUrl, string  strFileName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string strDir,strFilePage;
InBlock.gif            strDir 
= @"/htm/MakePage/";//更新到的文件夹
InBlock.gif
            strFilePage = Server.MapPath(strDir+strFileName);
InBlock.gif
InBlock.gif            StreamWriter sw
=null;
InBlock.gif            
//获得aspx的静态html
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!Directory.Exists(Server.MapPath(strDir)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Directory.CreateDirectory(Server.MapPath(strDir));
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(File.Exists(strFilePage))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    File.Delete(strFilePage);
ExpandedSubBlockEnd.gif                }

InBlock.gif                sw 
= new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("GB2312"));
InBlock.gif                Server.Execute(strPageUrl,sw);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                msg.ShowMsg(
"'"+strFileName+"'生成出错:"+ex.Message);
InBlock.gif                
return false;//生成到出错
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sw.Flush();
InBlock.gif                sw.Close();
InBlock.gif                sw 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return true;
ExpandedBlockEnd.gif        }
方法二:
ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 静态生成页面的方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strPageUrl">生成源</param>
ExpandedBlockEnd.gif        
/// <param name="strFileName">生成到</param>
None.gif          private   bool  MakePage( string  strPageUrl, string  strFileName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string strDir,strFilePage;
InBlock.gif            strDir 
= @"/htm/MakePage/";//更新到的文件夹
InBlock.gif
            strFilePage = Server.MapPath(strDir+strFileName);
InBlock.gif
InBlock.gif            StreamWriter sw
=null;
InBlock.gif            
//获得aspx的静态html
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!Directory.Exists(Server.MapPath(strDir)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Directory.CreateDirectory(Server.MapPath(strDir));
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(File.Exists(strFilePage))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    File.Delete(strFilePage);
ExpandedSubBlockEnd.gif                }

InBlock.gif                sw 
= new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("GB2312"));
InBlock.gif                System.Net.WebRequest wReq 
= System.Net.WebRequest.Create(strPageUrl); 
InBlock.gif                System.Net.WebResponse wResp 
= wReq.GetResponse(); 
InBlock.gif                System.IO.Stream respStream 
= wResp.GetResponseStream(); 
InBlock.gif                System.IO.StreamReader reader 
= new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312")); 
InBlock.gif                sw.Write( reader.ReadToEnd());    
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                msg.ShowMsg(
"'"+strFileName+"'生成出错:"+ex.Message);
InBlock.gif                
return false;//生成到出错
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sw.Flush();
InBlock.gif                sw.Close();
InBlock.gif                sw 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return true;
ExpandedBlockEnd.gif        }

总结:方法一只可用用虚拟路径(如:不能用 http://www.qq.com),而方法二相反。

转载于:https://www.cnblogs.com/QiuYun/archive/2007/01/16/621836.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值