- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Net;
- using System.IO;
- using System.Text;
- using System.Web.UI.HtmlControls;
- using System.Text.RegularExpressions;
-
-
-
-
- public class CreateHtml:System.Web.UI.Page
- {
- public CreateHtml()
- {
-
-
-
- }
-
-
-
-
-
- public bool Nei_Create(string strURL, string strRelPath)
- {
- string strFilePage;
-
- strFilePage = HttpContext.Current.Server.MapPath(strRelPath);
- StreamWriter sw = null;
-
- try
- {
-
- if (File.Exists(strFilePage))
- {
- File.Delete(strFilePage);
- }
- sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("gb2312"));
- System.Net.WebRequest wReq = System.Net.WebRequest.Create(strURL);
- System.Net.WebResponse wResp = wReq.GetResponse();
- System.IO.Stream respStream = wResp.GetResponseStream();
- System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
- string strTemp = reader.ReadToEnd();
-
- Regex r1 = new Regex("<input type=\"hidden\" name=\"__EVENTTARGET\".*/>", RegexOptions.IgnoreCase);
- Regex r2 = new Regex("<input type=\"hidden\" name=\"__EVENTARGUMENT\".*/>", RegexOptions.IgnoreCase);
- Regex r3 = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\".*/>", RegexOptions.IgnoreCase);
-
- Regex r4 = new Regex("<form .*id=\"form1\">", RegexOptions.IgnoreCase);
- Regex r5 = new Regex("</form>");
-
- Regex r6 = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\".*/>", RegexOptions.IgnoreCase);
- strTemp = r1.Replace(strTemp, "");
- strTemp = r2.Replace(strTemp, "");
- strTemp = r3.Replace(strTemp, "");
- strTemp = r4.Replace(strTemp, "");
- strTemp = r5.Replace(strTemp, "");
- strTemp = r6.Replace(strTemp, "");
-
- sw.Write(strTemp);
- }
- catch (Exception ex)
- {
- HttpContext.Current.Response.Write(ex.Message);
- HttpContext.Current.Response.End();
- return false;
- }
- finally
- {
- sw.Flush();
- sw.Close();
- sw = null;
- }
-
- return true;
- }
-
-
-
-
-
- public bool Wai_Create(string strURL, string strRelPath,string filename)
- {
- string strFilePage;
- strFilePage = strRelPath + "\\" + filename;
- StreamWriter sw = null;
-
- try
- {
- if (!Directory.Exists(strRelPath))
- {
- Directory.CreateDirectory(strRelPath);
- }
- if (File.Exists(strFilePage))
- {
- File.Delete(strFilePage);
- }
- sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("gb2312"));
- System.Net.WebRequest wReq = System.Net.WebRequest.Create(strURL);
- System.Net.WebResponse wResp = wReq.GetResponse();
- System.IO.Stream respStream = wResp.GetResponseStream();
- System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
- string strTemp = reader.ReadToEnd();
-
- Regex r1 = new Regex("<input type=\"hidden\" name=\"__EVENTTARGET\".*/>", RegexOptions.IgnoreCase);
- Regex r2 = new Regex("<input type=\"hidden\" name=\"__EVENTARGUMENT\".*/>", RegexOptions.IgnoreCase);
- Regex r3 = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\".*/>", RegexOptions.IgnoreCase);
-
- Regex r4 = new Regex("<form .*id=\"form1\">", RegexOptions.IgnoreCase);
- Regex r5 = new Regex("</form>");
-
- Regex r6 = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\".*/>", RegexOptions.IgnoreCase);
- strTemp = r1.Replace(strTemp, "");
- strTemp = r2.Replace(strTemp, "");
- strTemp = r3.Replace(strTemp, "");
- strTemp = r4.Replace(strTemp, "");
- strTemp = r5.Replace(strTemp, "");
- strTemp = r6.Replace(strTemp, "");
-
- sw.Write(strTemp);
- }
- catch (Exception ex)
- {
- HttpContext.Current.Response.Write(ex.Message);
- HttpContext.Current.Response.End();
- return false;
- }
- finally
- {
- sw.Flush();
- sw.Close();
- sw = null;
- }
-
- return true;
-
- }
- public void FilePicDelete(string path)
- {
- System.IO.FileInfo file = new System.IO.FileInfo(path);
- if (file.Exists)
- file.Delete();
- }
- }
-
-
- 用法:
- new CreateHtml().Nei_Create("http://localhost:4032/new5mdn/default.aspx", "default.htm");
-
-
-
-
最近学院网站要改版,于是有了这个想法,将首页生成静态文件,以提高访问速度。初步想法如下(只适应于首页静态文件生成): IIS中设置默认文档顺序为index.html,default.aspx 然后重写default.aspx的render方法:
protected override void Render(HtmlTextWriter writer) { //这是默认的Render实现,直接输出到writer中 //base.Render(writer); StringWriter html = new StringWriter(); HtmlTextWriter tw = new HtmlTextWriter(html); //通过base.Render()方法,把生成的HTML写入到我们定义的tw中 base.Render(tw); string path = Server.MapPath("index.html"); StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.Default); //写入到文件中 sw.Write(html.ToString()); sw.Close(); //写入到writer中,以输出页面的HTML //注意,这里不能再调用base.Render(writer),会出现错误 writer.Write(html.ToString()); }
这样在每次访问default.aspx的时候会在同目录下生成index.html文件,而一般用户访问时都会直接输入域名,如http:// 这样由于iis中的设置,用户默认访问到的都是index.html静态页面,应该能有效提高首页打开速度。因为首页更新不会太快,添加了新闻或其它在首页显示的内容的时候可以手动访问一下http:///default.aspx 这个页来重新生成index.html,或者在发布新闻后自动访问一下default.aspx页,这样就能做到index.html页内容及时更新,这种方法应该比outputcache更好,问题也会少。当然前提是首页没有各种postback事件,有登录框倒好办,直接把登录框改为常规的hmtl form,用post的方式post到登录页。
|
|