Aspx页面转静态页面

简介:
为客户在SharePoint的基础上搭建了一个门户,但是客户又反映说首页打开太慢,通过Fillder工具查看,页面打开速度大概在5秒左右。
  
   其实对于一个SharePoint站点来讲,打开速度在3-4秒还是一个可以接受的范围,但是我们的首页放了太多的内容,包括图片、Flash、还有N多个WebPart,以至于要不断的从数据库交互。
  
   首先想到的解决方案是在页面上加Cache,从Web层到数据层都可以考虑加Cache,但是这个方法很快就被否决了。因为SharePoint2007还不支持对自定义的页面加载Cache。
  
   第二个解决方案,生成静态页面,当用户访问时,让其访问静态页面。
  
   关于生成静态页面,方法还是很多的,简单列举下:
  
   1、ASP.NET下Page的RenderControl方法本身就是返回生成的HTML代码,该方法在前面的随笔中有提到;
  
   2、Page.Server.Execute(url,stringwriter)方法,只对该方法做了测试,有时间再深刻钻研;
  
   简单代码: 
  
  
  public bool ExecAspxToHtml()
   {
   StringWriter strWriterHTML = new StringWriter();
   System.Web.UI.Page aspxPage = new System.Web.UI.Page();
   aspxPage.Server.Execute(strAspxUrl, strWriterHTML);//将aspx页执行产生的html输出到StringWriter中
  
   StreamWriter streamWriter = new StreamWriter(strHtmlFile, true, System.Text.Encoding.GetEncoding("GB2312"));
   streamWriter.Write(strWriterHTML.ToString());
   strWriterHTML.Close();
   streamWriter.Close();
   return true;
  
   } 
   
  
   3、模拟HttpWebRequest,获取HttpWebResponse,采用这种方式来生成。
  
   简单说页面运行的机制:当我们在地址栏里输入网址后,第一次向服务器抛Request,客户端获取到该Request产生的Response后,对其内部的Html代码分析,对src,href等链接的文件则继续抛Request,获取Response,知道页面中需要的文件都下载到客户端,页面才能完全打开。
  
   决定了用第三种方案,对页面生成的HTMl代码,需要将其生成如 src 引用的链接文件同时下载到本地,对href引用的链接需要将其更改为绝对路径。需要考虑的问题:
  
   1、对页面内src 的Url进行分析,继续继续抛HttpWebRequest获取该文件,保存到本地,同事更改其src属性为本地相对路径。
  
   2、对页面的href 的url进行分析,在其url前加上网站的服务器名,成为绝对路径。
  
   最主要的是这两个问题,当然还有像Background中链接的图片。对于这些url,我都采用了 Regex的匹配以及替换的方法。感觉正则表达式还是很强大的。
  
   代码:
  
  
   
   //URL,用户名都是写在配置文件中的
   public void ExecuteAspxToHtml()
   {
   DistributeRequest(CreateWebRequest(strAspxUrl));
   }
  
   private HttpWebRequest CreateWebRequest(string url)
   {
   HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  
   request.ContentType = "application/x-www-form-urlencoded";
   request.Method = "GET";
   request.Credentials = CreateCredential();
  
   return request;
   }
  
   private ICredentials CreateCredential()
   {
   ICredentials credential;
   //获取配置文件中的值
   if (!string.IsNullOrEmpty(GetAppValue("Domain")))
   {
   credential = new NetworkCredential(GetAppValue("UserName"), GetAppValue("Password"), GetAppValue("Domain"));
   }
   else
   {
   credential = new NetworkCredential(GetAppValue("UserName"), GetAppValue("Password"));
   }
  
   return credential;
   }
  
   private void DistributeRequest(HttpWebRequest request)
   {
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   if (response.StatusCode == HttpStatusCode.OK)
   {
   if (Directory.Exists(strHtmlPath))
   {
   Directory.CreateDirectory(strHtmlPath);
   }
   BinaryReader rsReader = new BinaryReader(response.GetResponseStream());
   byte[] buffer = rsReader.ReadBytes((int)response.ContentLength);
  
   //对其内部URl进行分析,获取文件
   GetContainUrl(response.ContentType, ref buffer);
  
   if (!CompareFile(buffer))
   {
   FileStream fStream = new FileStream(strHtmlPath + strHtmlFile, FileMode.Create, FileAccess.Write);
   fStream.Write(buffer, 0, buffer.Length);
  
   fStream.Close();
   response.Close();
   }
  
   }
   }
   
   private void DistributeRequest(HttpWebRequest request, string filePath, string fileName)
   {
   try
   {
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   if (response.StatusCode == HttpStatusCode.OK)
   {
   if (Directory.Exists(filePath))
   {
   BinaryReader rsReader = new BinaryReader(response.GetResponseStream());
   byte[] buffer = rsReader.ReadBytes((int)response.ContentLength);
  
   FileStream fStream = new FileStream(filePath + fileName, FileMode.Create, FileAccess.Write);
   fStream.Write(buffer, 0, buffer.Length);
  
   fStream.Close();
   response.Close();
   }
   }
   }
   catch(Exception ex)
   {
   Console.WriteLine(ex.Message);
   Console.WriteLine(request.RequestUri.ToString());
   Console.WriteLine("URI错误");
   }
  
   }
  
   private void GetContainUrl(string ContentType, ref byte[] buffer)
   {
   Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
   string strbuffer = encode.GetString(buffer);
   
   strbuffer = HandleSrc(strbuffer);
   strbuffer = HandleHref(strbuffer);
  
   buffer = encode.GetBytes(strbuffer);
   } 
  
   /// <summary>
   /// 对以src=""形式的链接,获取其文件保存到本地
   /// </summary>
   /// <param name="strBuffer"></param>
   /// <returns></returns>
   private string HandleSrc(string strBuffer)
   {
   Console.WriteLine("Handle the src property to get file");
   Console.WriteLine("*************************************"); 
  
   Regex regex = new Regex(srcRegex, RegexOptions.IgnoreCase);
   MatchCollection matchs = regex.Matches(strBuffer);
   foreach (Match item in matchs)
   {
   if (item.Success)
   {
   string matchContent = item.Value;
   string filePathName = GetFilePathName(matchContent);
   string fileUrl = GetFileUrl(matchContent);
  
   string filePath = filePathName.Substring(0,filePathName.LastIndexOf('\\'));
  
   if (!Directory.Exists(strHtmlPath + filePath))
   {
   Directory.CreateDirectory(strHtmlPath + filePath);
   }
   //if (!IsUrlFileExist(strHtmlPath, filePathName))
   //{
   DistributeRequest(CreateWebRequest(fileUrl), strHtmlPath, filePathName);
   //}
   }
   }
   Console.WriteLine("*************************************************"); 
   Console.WriteLine("Handle the src property and get file completed");
   Console.WriteLine();
   //上边的循环可以放在处理中再抛Request,这里还没有改回来
   return regex.Replace(strBuffer, new MatchEvaluator(SrcMatchHandle));
   }
  
   private string HandleHref(string strBuffer)
   {
   Console.WriteLine("Handle the href property to change the relative link to absolute link");
   Console.WriteLine("************************************************************************"); 
  
   Regex regex = new Regex(hrefRegex, RegexOptions.IgnoreCase);
   //MatchCollection matchs = regex.Matches(strBuffer);
   return regex.Replace(strBuffer, new MatchEvaluator(HrefMatchHandle));
   }
   
   //返回要替换的字符串,即本地的相对路径
   //该方法在替换时也是遍历执行的,因此可以在该方法里获取文件
   private string SrcMatchHandle(Match match)
   {
   if (match.Success)
   {
   return GetFileRelatvePath(match.Value);
   }
   return match.Value;
   }
  
   private string HrefMatchHandle(Match match)
   {
   if (match.Success)
   {
   return match.Value.Insert(match.Value.IndexOf('/'), strAspxPath); 
   }
   return match.Value;
   }
   
  
   其实还是投机了,因为客户看中的只是首页,如果要让我去做所有页面的静态化,那可真是头疼,涉及到的各种各样的链接都需要考虑。
  
   现在开发工作已经完成,打算做成一个Windos服务部署到服务器,定时更新页面。还是第一次,正在研究,有经验的朋友可以探讨下。
  

   目标是为了解决SharePoint首页反应慢的,不过技术全是.Net的技术。  



本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2010/12/24/1916391.html,如需转载请自行联系原作者

目录
相关文章
|
.NET Windows 开发框架
|
数据安全/隐私保护
怎么将aspx网站改为静态页面?
首先思路是在后台点击“生成静态页面” 转换为静态页面 protected void Button2_Click(object sender, EventArgs e) { DataSet ds = Getyuqian().
1630 0
|
Web App开发 XML .NET
生成静态页面
这里给出5种方案,希望可以帮到你如何生成静态页: 方案1: /// /// 传入URL返回网页的html代码 /// /// URL /// public static string getUrltoHtml(string Url) { errorMsg = ""; try { System.
819 0
|
4月前
|
移动开发 数据安全/隐私保护 HTML5
|
.NET 开发框架 C++
aspx网页控件
aspx网页控件 按钮、下拉列表、单选框等工具称之为控件。以button控件为例。 vs中view|Tool Box 可以调出工具箱,可以拖button控件到asp页面中。 那么自动生成了 &lt;asp:Button ID="Button1" runat="server" Text="button" /&gt;。 双击添加的按钮,则自动生成单击事件。实现跳转百度语句为:
1283 0