ASP.NET生成HTML静态页的一个类
来源:百度文库 编辑:神马文学网 时间:2024/04/25 23:34:35
C#代码
- 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;
- ///
- ///CreateHtml 的摘要说明
- ///
- public class CreateHtml:System.Web.UI.Page
- {
- public CreateHtml()
- {
- //
- //TODO: 在此处添加构造函数逻辑
- //
- }
- ///
- /// 生成静态页面,生成位置是本项目下
- ///
- /// 请求的URL
- /// 创建的路径及文件名,路径为相对路径
- public bool Nei_Create(string strURL, string strRelPath)
- {
- string strFilePage;
- strFilePage = HttpContext.Current.Server.MapPath(strRelPath);
- StreamWriter sw = null;
- //获得aspx的静态html
- 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("", RegexOptions.IgnoreCase);
- Regex r2 = new Regex("", RegexOptions.IgnoreCase);
- Regex r3 = new Regex("", RegexOptions.IgnoreCase);
- Regex r4 = new Regex(", RegexOptions.IgnoreCase);
- Regex r5 = new Regex("");
- Regex r6 = new Regex("", 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;
- }
- ///
- /// 生成静态页面,生成位置不在本项目下
- ///
- /// 请求的URL
- /// 创建的路径及文件名,路径为绝对路径
- public bool Wai_Create(string strURL, string strRelPath,string filename)
- {
- string strFilePage;
- strFilePage = strRelPath + "\\" + filename;
- StreamWriter sw = null;
- //获得aspx的静态html
- 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("", RegexOptions.IgnoreCase);
- Regex r2 = new Regex("", RegexOptions.IgnoreCase);
- Regex r3 = new Regex("", RegexOptions.IgnoreCase);
- Regex r4 = new Regex(", RegexOptions.IgnoreCase);
- Regex r5 = new Regex("");
- Regex r6 = new Regex("", 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();
- }
- }
- 用法:
C#代码- 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://domain.com 这样由于iis中的设置,用户默认访问到的都是index.html静态页面,应该能有效提高首页打开速度。因为首页更新不会太快,添加了新闻或其它在首页显示的内容的时候可以手动访问一下http://domain.com/default.aspx 这个页来重新生成index.html,或者在发布新闻后自动访问一下default.aspx页,这样就能做到index.html页内容及时更新,这种方法应该比outputcache更好,问题也会少。当然前提是首页没有各种postback事件,有登录框倒好办,直接把登录框改为常规的hmtl form,用post的方式post到登录页。
ASP.NET生成HTML静态页的一个类
不用模板,只用ASP+FSO生成静态HTML页的一个方法(对于内容密集型页面特别适用)
Asp.NET 生成静态页面并分页的代码实现
Asp.NET 生成静态页面并分页的代码00
ASP生成静态HTML文件大荟萃【一】
ASP生成静态网页的方法ASP技术【推荐】
ASP生成静态网页的方法ASP技术【推荐】
JSP生成静态HTML页面的几种方法
ASP+模版技术生成静态HTML网页实例 - 恬悟 -- IT博客-中国最具人气的IT博客-赛迪网IT人家园
通用asp生成html代码
ASP生成HTML-另一篇
使用ASP生成HTML文件
通用asp生成html代码
通用asp生成html代码
动态生成的ASP文件转换成静态的格式 Web 开发 / ASP
在ASP.NET中设置html文件的访问验证
asp.net(C#) 生成随机验证码的代码
生成静态页
php生成静态页
实例演示:使用ASP生成HTML文件
通用asp生成html代码--学无止境
ASP.NET中随机数生成及应用
生成HTML的方法
php生成静态页,分页