页面静态化--总结

为了让公司平台访问速度 更快 我学习了 页面的静态化

这里用的是其中一种 比较简单的方式

方法如下:

第一为了方便 我建一个basepage 的类 把具体的方法写在父类

代码:

using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.IO; /// <summary> ///源码转载,注释自加 /// </summary> public class BasePage : System.Web.UI.Page { public BasePage() { } /// <summary> /// 加载事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { //将newurl赋值成这个页面 Freeze(string.Format(@"{0}.htm", Request.Url.ToString())); } /// <summary> /// 将写好的html文件输出到页面中的方法 /// </summary> /// <param name="writer"></param> protected override void Render(HtmlTextWriter writer) { //已经赋值了的freeze if (freeze) { MyHtmlFileCreator htmlFile = new MyHtmlFileCreator(); base.Render(htmlFile.RenderHere); newUrl = HttpContext.Current.Request.Url.AbsolutePath.ToString(); newUrl = newUrl.Replace(".aspx", ".htm");//将路径newurl变成.html这样的格式 htmlFile.WriteHTMLFile(Server.MapPath(newUrl)); //将原来的页面但参数传入 并且输出和保存html页面 Response.Redirect(newUrl, true);//输出页面 } else { base.Render(writer); } } protected void Freeze() { freeze = true; } protected void Freeze(string toUrl) { freeze = true; NewUrl = toUrl; } private bool freeze = false; private string newUrl; internal string NewUrl { get { return newUrl; } set { newUrl = value; } } } /// <summary> /// 这个类是写html页面的具体方法 /// </summary> internal class MyHtmlFileCreator { private StringWriter html; private MyHtmlTextWriter htmlWriter; class MyHtmlTextWriter : HtmlTextWriter { internal MyHtmlTextWriter(TextWriter tw) : base(tw) { } } // publish the HTMLwriter internal HtmlTextWriter RenderHere { get { return htmlWriter; } } internal MyHtmlFileCreator() { html = new StringWriter(); htmlWriter = new MyHtmlTextWriter(html); } /// <summary> /// 这是写页面的具体的方法 /// </summary> /// <param name="virtualFileName"></param> internal void WriteHTMLFile(string virtualFileName) { StringReader sr = new StringReader(html.ToString());//读出页面 StringWriter sw = new StringWriter(); string htmlLine = sr.ReadLine(); while (htmlLine != null) { sw.WriteLine(htmlLine); htmlLine = sr.ReadLine(); } StreamWriter fs = new StreamWriter(virtualFileName, false, System.Text.Encoding.UTF8); fs.Write(sw.ToString()); fs.Close(); } }

第二:现在在一般的页面中 重写父类中的page_load 方法就行了.

代码如下:

protected void Page_Load(object sender, EventArgs e) { base.Page_Load(sender,e); }

基本就完成了...不过现在访问要访问的是aspx页面才可以生成.htm文件.有什么办法可以直接就访问html文件呢

第三:配置iis

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值