当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET生成静态HTML页面并分别按年月目录存放

ASP.NET
.Net中使用com组件后发生System.ArithmeticException异常的解决办法
SQL Server.net 和 OLE DB.net连接数据库的比较
后台更新DataTable行内容的方法
敏捷软件开发(原则,模式与实践)笔记1
确保文本框输入值为数值的代码
XML和数据库之间相互的映射
让你的.NET程序兼容不同版本的Dll文件。
.NET 的数据访问应用程序块(Data Access Application Block)
用控件仅一条指令实现界面换肤和多语言版本(YFSkins)
Microsoft User Interface Process Application Block 研究(3)
分享:处理Excel方法小结
基于ASP.NET实现全球化
.net 里面 protected private 的变量也可以访问(新发现)。
关于C#中{0}和{1}的问题初次在此发贴,问题对你易对我难,求救了
使用C#代码实现增加用户帐号
全世界都在关注-微软重大产品发布
教你做Rational Rose(UML Design)
OLE DB取得数据库的架构信息
VB 从零开始编外挂(三)
XPath序列之四

ASP.NET生成静态HTML页面并分别按年月目录存放


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 83 ::
收藏到网摘: n/a

一说到新闻系统的话,一定会谈到静态页面生成的,因为静态页面不但是读取速度快,而且又安全;

静态页面的生成不管是小到现在的企业网站大至网易,QQ等门户都用到了;

那么我们如何来生成静态页呢?

以什么方式生成静态页面呢……

在生成静态页面的时候有那些是要注意的呢:

静态页面命名

统一存放目录

静态页面模板

页面生成

一般来说,在原来新闻系统的基础上我们可以根据GET此页面请求的内容再生成(比如:http;//www.test.com/news.aspx?id=1,GET此页面代码直接写至一个文本文件并以HTML命名即可);

在这里我所采用的是模板生成,先用DW做一个网页模板,将标题,内容等将要动态实现的内容先以$Title$等替换,等在生成的时候替换成新闻的内容;

命名:在生成的时候一般来说是采用新闻发布的时间转换成的字符串,这个是不会重复的。另外我还按年份月份把这些静态文件存放在不同的目录,以便于管理,在这里根据一个新闻的ID调用方法WriteNews()给定参数ID,它就会根据此ID从数据库中读取内容,再根据静态模板页面html/test.html生成新的静态页面存放在相应年份月份的目录

好了,下面是代码:

以下为引用的内容:

using System;
using System.IO;
using System.Web;
using System.Text;
namespace PowerLeader.Components
...{
    /**//// <summary>
    /// WriteTOHtml 的摘要说明。
    /// </summary>
    public class WriteTOHtml
    ...{
        public WriteTOHtml()
        ...{
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public static void WriteNews(int id)
        ...{
            News news = new News();           
            News.NewsDetails newsDetails = new PowerLeader.Components.News.NewsDetails();
            newsDetails = news.GetNews(id);
            bool flag;
            flag = WriteFile(newsDetails);
        }

        public static bool WriteFile(News.NewsDetails newsDetails)
        ...{
            Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/PowerLeader/html/"+newsDetails.addtime.ToString("yyyy")+"/"+newsDetails.addtime.ToString("MM")));
            string path = HttpContext.Current.Server.MapPath("../html/"+newsDetails.addtime.ToString("yyyy")+"/"+newsDetails.addtime.ToString("MM")+"/");
            Encoding code = Encoding.GetEncoding("gb2312");
            // 读取模板文件
            string temp = HttpContext.Current.Server.MapPath("../html/text.html");
            StreamReader sr = null;
            StreamWriter sw = null;
            string stringTempCode = ""; 
            try
            ...{
                sr = new StreamReader(temp, code);
                stringTempCode = sr.ReadToEnd(); // 读取文件
            }
            catch(Exception exp)
            ...{
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }  
            string htmlFileName = newsDetails.addtime.ToString("yyyyMMddHHmmss") + ".html";
            // 替换内容
            // 这时,模板文件已经读入到名称为str的变量中了
            stringTempCode = stringTempCode.Replace("$PageTitle$","抗战OnLine官方网站...");
            stringTempCode = stringTempCode.Replace("$Type$",newsDetails.type.ToString().Trim());
            stringTempCode = stringTempCode.Replace("$Author$",newsDetails.author.ToString().Trim());
            stringTempCode = stringTempCode.Replace("$From$",newsDetails.from.Trim());
            stringTempCode = stringTempCode.Replace("$Time$",newsDetails.addtime.ToString().Trim());
            stringTempCode = stringTempCode.Replace("$Title$",newsDetails.title.Trim());
            stringTempCode = stringTempCode.Replace("$Content$",newsDetails.content);
            // 写文件
            try
            ...{
                sw = new StreamWriter(path + htmlFileName , false, code);
                sw.Write(stringTempCode);
                sw.Flush();
            }
            catch(Exception ex)
            ...{
                HttpContext.Current.Response.Write(ex.Message);
                HttpContext.Current.Response.End();
            }
            finally
            ...{
                sw.Close();
            }
            return true;
        }
    }
}