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

两种方法

方法一:

         /// <summary>
        
/// 静态生成页面的方法
        
/// </summary>
        
/// <param name='strPageUrl'>生成源</param>
        
/// <param name='strFileName'>生成到</param>

         private   bool  MakePage( string  strPageUrl, string  strFileName)
        
{
            
string strDir,strFilePage;
            strDir 
= @'/htm/MakePage/';//更新到的文件夹
            strFilePage = Server.MapPath(strDir+strFileName);

            StreamWriter sw
=null;
            
//获得aspx的静态html
            try
            
{
                
if (!Directory.Exists(Server.MapPath(strDir)))
                
{
                    Directory.CreateDirectory(Server.MapPath(strDir));
                }

                
if(File.Exists(strFilePage))
                
{
                    File.Delete(strFilePage);
                }

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

            
catch(Exception ex)
            
{
                msg.ShowMsg(
'''+strFileName+''生成出错:'+ex.Message);
                
return false;//生成到出错
            }

            
finally
            
{
                sw.Flush();
                sw.Close();
                sw 
= null;
            }


            
return true;
        }
方法二:
         /// <summary>
        
/// 静态生成页面的方法
        
/// </summary>
        
/// <param name='strPageUrl'>生成源</param>
        
/// <param name='strFileName'>生成到</param>

         private   bool  MakePage( string  strPageUrl, string  strFileName)
        
{
            
string strDir,strFilePage;
            strDir 
= @'/htm/MakePage/';//更新到的文件夹
            strFilePage = Server.MapPath(strDir+strFileName);

            StreamWriter sw
=null;
            
//获得aspx的静态html
            try
            
{
                
if (!Directory.Exists(Server.MapPath(strDir)))
                
{
                    Directory.CreateDirectory(Server.MapPath(strDir));
                }

                
if(File.Exists(strFilePage))
                
{
                    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)
            
{
                msg.ShowMsg(
'''+strFileName+''生成出错:'+ex.Message);
                
return false;//生成到出错
            }

            
finally
            
{
                sw.Flush();
                sw.Close();
                sw 
= null;
            }


            
return true;
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值