三:理解Page类的运行机制(例:在render方法中生成静态文件)
2009-01-14 20:58
375 查看
我这里只写几个常用的事件
1.OnPreInit:此事件后将加载个性化信息和主题
2.OnInit:初始化页面中服务器控件的默认值但控件的状态没有加载,没有创建控件树
3.OnPreLoad:控件完成状态和回传数据的加载
4.Page_Load:此事件是在OnInit中订阅的
5.Render:呈现最终页面的内容
假设有一个文章数据库
以前都是通过article.aspx?id=123的动态形式访问的
现在我们想要减轻服务器压力,把文章生成静态文件
先看article.aspx的程序
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
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.IO;//StringWriter名称空间
namespace _1
{
public partial class article : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(Request["id"]))
Label1.Text = "文章内容为:"+ Request["id"].ToString();
}
protected override void Render(HtmlTextWriter writer)
{
StringWriter sw = new StringWriter();//这个和StringBuilder没太大区别
HtmlTextWriter htmlw = new HtmlTextWriter(sw);
base.Render(htmlw);//不用传递进来的writer
htmlw.Flush();
htmlw.Close();
string PageContent = sw.ToString();
string path = Server.MapPath("~/Article/");
string pageurl = xland.MyModule.GetFileName(HttpContext.Current);
using (StreamWriter stringWriter = File.AppendText(path + pageurl))
{
stringWriter.Write(PageContent);
}
Response.Write(PageContent);
}
}
}
我们还是通过自定义httpModules来实现url重写
webconfig文件没有太大变化
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"></compilation>
<httpModules>
<add name="myModule" type="xland.MyModule" />
</httpModules>
</system.web>
</configuration>
MyModule程序
using System;
using System.Collections.Generic;
using System.Web;//引用web命名空间
using System.Text;
using System.IO;
namespace xland
{
public class MyModule:IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest +=new EventHandler(context_BeginRequest);
}
public void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//AppRelativeCurrentExecutionFilePath这里不包括传过来的参数
if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx"))
{
string fileurl = "~/article/" + GetFileName(context);
if (File.Exists(context.Server.MapPath(fileurl)))
{
context.RewritePath(fileurl, false);
}
}
}
public static string GetFileName(HttpContext context)
{
return context.Request.AppRelativeCurrentExecutionFilePath.ToLower().Replace(".aspx", "").Replace("~/", "") + context.Request.Url.Query.Replace("?id=", "_") + ".html";
}
public void Dispose() { }
}
}
注释就不多写了,相信大家能看懂
这个示例程序只是为了说明page类的Render事件
如果要用到项目中,请慎重
因为会造成大量的服务器IO
而且这也不是生成静态页面的最佳方案
1.OnPreInit:此事件后将加载个性化信息和主题
2.OnInit:初始化页面中服务器控件的默认值但控件的状态没有加载,没有创建控件树
3.OnPreLoad:控件完成状态和回传数据的加载
4.Page_Load:此事件是在OnInit中订阅的
5.Render:呈现最终页面的内容
假设有一个文章数据库
以前都是通过article.aspx?id=123的动态形式访问的
现在我们想要减轻服务器压力,把文章生成静态文件
先看article.aspx的程序
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
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.IO;//StringWriter名称空间
namespace _1
{
public partial class article : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(Request["id"]))
Label1.Text = "文章内容为:"+ Request["id"].ToString();
}
protected override void Render(HtmlTextWriter writer)
{
StringWriter sw = new StringWriter();//这个和StringBuilder没太大区别
HtmlTextWriter htmlw = new HtmlTextWriter(sw);
base.Render(htmlw);//不用传递进来的writer
htmlw.Flush();
htmlw.Close();
string PageContent = sw.ToString();
string path = Server.MapPath("~/Article/");
string pageurl = xland.MyModule.GetFileName(HttpContext.Current);
using (StreamWriter stringWriter = File.AppendText(path + pageurl))
{
stringWriter.Write(PageContent);
}
Response.Write(PageContent);
}
}
}
我们还是通过自定义httpModules来实现url重写
webconfig文件没有太大变化
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"></compilation>
<httpModules>
<add name="myModule" type="xland.MyModule" />
</httpModules>
</system.web>
</configuration>
MyModule程序
using System;
using System.Collections.Generic;
using System.Web;//引用web命名空间
using System.Text;
using System.IO;
namespace xland
{
public class MyModule:IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest +=new EventHandler(context_BeginRequest);
}
public void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//AppRelativeCurrentExecutionFilePath这里不包括传过来的参数
if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx"))
{
string fileurl = "~/article/" + GetFileName(context);
if (File.Exists(context.Server.MapPath(fileurl)))
{
context.RewritePath(fileurl, false);
}
}
}
public static string GetFileName(HttpContext context)
{
return context.Request.AppRelativeCurrentExecutionFilePath.ToLower().Replace(".aspx", "").Replace("~/", "") + context.Request.Url.Query.Replace("?id=", "_") + ".html";
}
public void Dispose() { }
}
}
注释就不多写了,相信大家能看懂
这个示例程序只是为了说明page类的Render事件
如果要用到项目中,请慎重
因为会造成大量的服务器IO
而且这也不是生成静态页面的最佳方案
相关文章推荐
- 三:理解Page类的运行机制(例:在render方法中生成静态文件)
- 三:理解Page类的运行机制(例:在render方法中生成静态文件)
- python打包生成的exe文件运行时提示缺少模块的解决方法
- ASP.NET 2.0中,生成一个静态文件的方法
- Webwork生成静态文件的简单方法
- C#利用定时任务插件实现在后台线程中批量把动态文件生成静态文件方法
- Asp.net生成htm静态文件的源代码 2种方法
- php生成静态文件的多种方法分享
- Node.js+jade+mongodb+mongoose实现爬虫分离入库与生成静态文件的方法
- 用include()和ob_get_contents( )方法 生成静态文件
- FSO方法列表 ----静态文件生成
- 四:理解Page类的运行机制(例:基于PageStatePersister的页面状态存取)
- Linux下静态库.a文件生成方法
- jetty java文件无法删除 java文件占用 delete无效 运行时锁定静态资源的解决方法
- IOS获取各个文件目录路径的方法以及相关沙盒机制理解
- PHP中实现生成静态文件之简单方法一
- asp网页生成静态htm文件的方法
- php生成静态文件的方法
- 执行JSP后生成HTML静态文件【方法二】