用模板生成静态页
这个小project主要是为了练习用模板生成静态页
使用ASP.NET模版生成HTML静态页面并不是难事,主要是使各个静态页面间的关联和链接如何保持完整
主要实现的功能是,后台添加新闻,生成.html文件,前台访问相应的静态页
开发工具VS2012+SQL2008
优点
1. 可以建立非常复杂的页面,利用包含js文件的方法,在js文件内加入document.write()方法可以在所有页面内加入如页面头,广告等内容。
2. 静态html文件利用MS Windows2000的Index Server可以建立全文搜索引擎,利用asp.net可以以DataTable的方式得到搜索结果。而Win2000的Index服务无法查找xml文 件的内容。如果包括了数据库搜索与Index索引双重查找,那么此搜索功能将非常强大。
3. 节省服务器的负荷,请求一个静态的html文件比一个aspx文件服务器资源节省许多。
缺点
思路二: 如果用硬编码的方式,工作量非常大,需要非常多的html代码。调试困难。而且使用硬编码生成的html样式无法修改,如果网站更换样式,那么必须得重新编码,给后期带来巨大的工作量。
新建了一个自带的Web From项目
主要界面如图:
添加新闻之后,所生成的页面都是.html文件
最后详细的新闻页面如下:
主要注意的事项有:
1.将不同种类的新闻,放在不同的文件夹下(这个在早期就要设计好)
2.在创建的模板html文件,对于css,js的路径要注意
看下主要的代码:(部分来自网络)
模板:
< header > < div class ="content-wrapper" > < div class ="float-left" > < p class ="site-title" > < a href ="./" > your logo here </ a > </ p > </ div > < div class ="float-right" > < section id ="login" > < ul > < li >< a href ="Account/Register.aspx" id ="ctl10_registerLink" > Register </ a ></ li > < li >< a href ="Account/Login.aspx" id ="ctl10_loginLink" > Log in </ a ></ li > </ ul > </ section > < nav > < ul id ="menu" > < li >< a href ="./" > Home </ a ></ li > < li >< a href ="About.aspx" > About </ a ></ li > < li >< a href ="Contact.aspx" > Contact </ a ></ li > </ ul > </ nav > </ div > </ div > </ header > < div id ="body" > < section class ="contact" > < header > 新闻标题 </ header > < p > $Title$ </ p > </ section > < section class ="contact" > < header > 新闻内容 </ header > < p > $Content$ </ p > </ section > < section class ="contact" > < header > 记者 </ header > < p > $Author$ </ p > </ section > < section class ="contact" > < header > 发布时间 </ header > < p > $WriterTimes$ </ p > </ section > </ div > < footer > < div class ="content-wrapper" > < div class ="float-left" > < p > My ASP.NET Application-大蜗牛 </ p > </ div > </ div > </ footer >
相关生成html文件的方法:
private bool WriteFile( string htmlName, string strArticle, string strTitle, string strContent, string strAuthor, string htmlFileName) { string path = HttpContext.Current.Server.MapPath( " newspages/ " ); string sql; Encoding code = Encoding.GetEncoding( " gb2312 " ); // 读取模版文件 string temp = HttpContext.Current.Server.MapPath( " NewsTemplate.html " ); try { sr = new StreamReader(temp, code); str = sr.ReadToEnd(); sr.Close(); } catch (Exception) { sr.Close(); throw ; return false ; } // 替换内容 str = str.Replace( " $ShowArticle$ " , strArticle); str = str.Replace( " $Title$ " , strTitle); str = str.Replace( " $Content$ " , strContent); str = str.Replace( " $Author$ " , strAuthor); str = str.Replace( " $WriterTimes$ " , System.DateTime.Now.ToShortDateString()); ViewState[ " P " ] = path + htmlName; sql = " insert into News(title,[content],author,htmlname) values (' " + strTitle + " ',' " + strContent + " ',' " + strAuthor + " ',' " + htmlFileName + " ') " ; try { sw = new StreamWriter(path + htmlName, false , code); sw.Write(str); sw.Flush(); sh.Add(sql); } catch (Exception) { return false ; throw ; sw.Close(); } return true ; }
源代码下载(数据库在App_Data)
作者:大蜗牛 出处:http://www.cnblogs.com/lixiaofei/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类: .Net
标签: 静态页
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息