您的位置:首页 > 其它

页面静态化的方法

2009-01-09 11:12 246 查看
-:通过404错误

Code
1 public class MyHttpModule : IHttpModule
2 {
3 /// <summary>
4 ///初始化类
5 /// </summary>
6 /// <param name="application"></param>
7 public void Init(HttpApplication application)
8 {
9 //为事件添加委托
10 application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
11 application.EndRequest += (new EventHandler(this.Application_EndRequest));
12 }
13
14 /// <summary>
15 /// 应用程序开始请求时
16 /// </summary>
17 /// <param name="source"></param>
18 /// <param name="e"></param>
19 private void Application_BeginRequest(Object sender, EventArgs e)
20 {
21 //在此进行静态化
22 HttpApplication application = (HttpApplication)sender;
23 HttpContext context = application.Context;
24 context.Response.Filter = new FilterHtml(context.Response.Filter);
25
26 }
27
28 /// <summary>
29 /// 应用程序结束请求时
30 /// </summary>
31 /// <param name="source"></param>
32 /// <param name="e"></param>
33 private void Application_EndRequest(Object sender, EventArgs e)
34 {
35 //代码
36 }
37
38
39 /// <summary>
40 /// 注销类
41 /// </summary>
42 public void Dispose()
43 {
44
45 }
46
47 }
48
49
50 /// <summary>
51 ///FilterHtml 的摘要说明 (静态化)
52 /// </summary>
53 public class FilterHtml : Stream
54 {
55 private Stream _sink;
56 private long _position;
57
58 public FilterHtml(Stream sink)
59 {
60 this._sink = sink;
61 }
62
63 public override bool CanRead
64 {
65 get
66 {
67 return true;
68 }
69 }
70
71 public override bool CanSeek
72 {
73 get
74 {
75 return true;
76 }
77 }
78
79 public override bool CanWrite
80 {
81 get
82 {
83 return true;
84 }
85 }
86
87 public override long Length
88 {
89 get
90 {
91 return 0;
92 }
93 }
94
95 public override long Position
96 {
97 get
98 {
99 return this._position;
100 }
101 set
102 {
103 this._position = value;
104 }
105 }
106
107 public override long Seek(long offset, SeekOrigin direction)
108 {
109 return this._sink.Seek(offset, direction);
110 }
111
112 public override void SetLength(long length)
113 {
114 this._sink.SetLength(length);
115 }
116
117 public override void Close()
118 {
119 this._sink.Close();
120 }
121
122 public override void Flush()
123 {
124 this._sink.Flush();
125 }
126
127 public override int Read(byte[] buffer, int offset, int count)
128 {
129 return this._sink.Read(buffer, offset, count);
130 }
131
132 public override void Write(byte[] buffer, int offset, int count)
133 {
134 if (HttpContext.Current.Response.ContentType == "text/html")
135 {
136 Encoding e = Encoding.GetEncoding(HttpContext.Current.Response.Charset);
137 string s = e.GetString(buffer, offset, count);
138 s = s.Replace("a.aspx", string.Empty);
139 this._sink.Write(e.GetBytes(s), 0, e.GetByteCount(s));
140 }
141 else
142 {
143 this._sink.Write(buffer, offset, count);
144 }
145 }
146
147 }
148
149

在web.config中添加

<httpModules>
<add name="MyHttpModule" type="MyHttpModule"/>
</httpModules>

IHTTPModule的实现URL重写2008-06-21 06:18

原理,
1。匹配访问地址,
2. 在public void ProcessRequest(HttpContext context)中
根据访问规则用
context.Server.Execute(path);
执行请求页面

<system.web></system.web>中加入 :

<httpHandlers>
<add verb="*" path="匹配的文件" type="类空间.类全名"/>
</httpHandlers>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: