现在的位置: 首页 > 综合 > 正文

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

2012年12月22日 ⁄ 综合 ⁄ 共 2905字 ⁄ 字号 评论关闭
两种方法

方法一:

        /// <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),而方法二相反。

【上篇】
【下篇】

抱歉!评论已关闭.