页面静态化的方法
-:通过404错误
Code
Code
1 //页面静态化的思路
2
3 //首先得捕获404错误
4 //跳转到相应的aspx页面
5 //对这个aspx页面进行静态化 并在另一目录中生成html文件
6 //跳转到该html文最后可以通过微软自带的urlrewrite设置相应的正则表达式
7
8
9 string htmlFilePath = string.Empty;//当前要转到的静态页面地址
10
11
12 protected void Page_Load(object sender, EventArgs e)
13 {
14
15 if (Request.Url.ToString().IndexOf("404") > -1)
16 {
17 string[] Urls = Request.Url.ToString().Split(';');
18
19 htmlFilePath = Urls[1]; //当前要转到的静态页面地址
20 string filePrefix = ""; //页面名称+类型(html)
21 string filename = ""; //页面名称
22 string regexHtmlFileName = "\\.html";
23 Regex fileNameRegex = new Regex(regexHtmlFileName);
24
25 //判断是否为静态页面
26 if (fileNameRegex.IsMatch(htmlFilePath))
27 {
28 #region 分析文件名
29 string[] htmlFileNames = htmlFilePath.Split(new char[] { '/' });
30
31 filename = htmlFileNames[htmlFileNames.Length - 1];
32 filePrefix = filename.Substring(0, filename.LastIndexOf('.')).ToLower();
33 #endregion
34
35 }
36 else
37 {
38 Response.Write("对不起!你访问的页面不存在!");
39 Response.End();
40 }
41
42 //获取aspx页面的html
43 string html = GetHtml(htmlFilePath.Replace("html", "aspx"), "GB2312");
44
45 //替换动态信息
46 html = html.Replace("aspx", "html");
47 htmlFilePath = htmlFilePath.Substring(0, htmlFilePath.Length - filename.Length);
48
49 //创建html文件
50 CreateHtml(html, "html/" + filename);
51
52 //转到html页面
53 Response.Redirect(htmlFilePath.ToString() + "html/" + filename);
54
55
56 }
57
58
59
60
61 }
62
63
64 /// <summary>
65 /// 获取html代码 从aspx中
66 /// </summary>
67 /// <param name="url"></param>
68 /// <param name="code"></param>
69 /// <returns></returns>
70 public static String GetHtml(string url, string code)
71 {
72 string result = url;
73 WebResponse objResponse;
74 WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
75 objResponse = objRequest.GetResponse();
76
77 using (StreamReader sr = new StreamReader(objResponse.GetResponseStream(), System.Text.Encoding.GetEncoding(code)))
78 {
79 result = sr.ReadToEnd();
80 sr.Close();
81 }
82
83 return result;
84
85 }
86
87
88 //创建静态网页
89 public static bool CreateHtml(string htmlCode, string name)
90 {
91
92 using (System.IO.StreamWriter sw = new StreamWriter(Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, name), false, System.Text.Encoding.GetEncoding("GB2312")))
93 {
94 sw.Write(htmlCode);
95 sw.Close();
96 sw.Dispose();
97 return true;
98 }
99 }
Code
1 //页面静态化的思路
2
3 //首先得捕获404错误
4 //跳转到相应的aspx页面
5 //对这个aspx页面进行静态化 并在另一目录中生成html文件
6 //跳转到该html文最后可以通过微软自带的urlrewrite设置相应的正则表达式
7
8
9 string htmlFilePath = string.Empty;//当前要转到的静态页面地址
10
11
12 protected void Page_Load(object sender, EventArgs e)
13 {
14
15 if (Request.Url.ToString().IndexOf("404") > -1)
16 {
17 string[] Urls = Request.Url.ToString().Split(';');
18
19 htmlFilePath = Urls[1]; //当前要转到的静态页面地址
20 string filePrefix = ""; //页面名称+类型(html)
21 string filename = ""; //页面名称
22 string regexHtmlFileName = "\\.html";
23 Regex fileNameRegex = new Regex(regexHtmlFileName);
24
25 //判断是否为静态页面
26 if (fileNameRegex.IsMatch(htmlFilePath))
27 {
28 #region 分析文件名
29 string[] htmlFileNames = htmlFilePath.Split(new char[] { '/' });
30
31 filename = htmlFileNames[htmlFileNames.Length - 1];
32 filePrefix = filename.Substring(0, filename.LastIndexOf('.')).ToLower();
33 #endregion
34
35 }
36 else
37 {
38 Response.Write("对不起!你访问的页面不存在!");
39 Response.End();
40 }
41
42 //获取aspx页面的html
43 string html = GetHtml(htmlFilePath.Replace("html", "aspx"), "GB2312");
44
45 //替换动态信息
46 html = html.Replace("aspx", "html");
47 htmlFilePath = htmlFilePath.Substring(0, htmlFilePath.Length - filename.Length);
48
49 //创建html文件
50 CreateHtml(html, "html/" + filename);
51
52 //转到html页面
53 Response.Redirect(htmlFilePath.ToString() + "html/" + filename);
54
55
56 }
57
58
59
60
61 }
62
63
64 /// <summary>
65 /// 获取html代码 从aspx中
66 /// </summary>
67 /// <param name="url"></param>
68 /// <param name="code"></param>
69 /// <returns></returns>
70 public static String GetHtml(string url, string code)
71 {
72 string result = url;
73 WebResponse objResponse;
74 WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
75 objResponse = objRequest.GetResponse();
76
77 using (StreamReader sr = new StreamReader(objResponse.GetResponseStream(), System.Text.Encoding.GetEncoding(code)))
78 {
79 result = sr.ReadToEnd();
80 sr.Close();
81 }
82
83 return result;
84
85 }
86
87
88 //创建静态网页
89 public static bool CreateHtml(string htmlCode, string name)
90 {
91
92 using (System.IO.StreamWriter sw = new StreamWriter(Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, name), false, System.Text.Encoding.GetEncoding("GB2312")))
93 {
94 sw.Write(htmlCode);
95 sw.Close();
96 sw.Dispose();
97 return true;
98 }
99 }
二:通过重写IHttpModule
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
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> |