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的技术。

优质内容筛选与推荐>>
1、SendMessage与PostMessage的区别(转)
2、二 Channel
3、ASP.NET操作Oracle知识记录(采用ODP.NET)
4、平衡二叉树AVL - 插入节点后旋转方法分析
5、CSS滤镜属性详解


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号