天天看点

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

   if (Directory.Exists(filePath))

   FileStream fStream = new FileStream(filePath + fileName, FileMode.Create, FileAccess.Write);

   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)

   return match.Value.Insert(match.Value.IndexOf('/'), strAspxPath); 

   其实还是投机了,因为客户看中的只是首页,如果要让我去做所有页面的静态化,那可真是头疼,涉及到的各种各样的链接都需要考虑。

   现在开发工作已经完成,打算做成一个Windos服务部署到服务器,定时更新页面。还是第一次,正在研究,有经验的朋友可以探讨下。

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

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