本文实例讲述了ASP.NET编程简单实现生成静态页面的方法。分享给大家供大家参考,具体如下:
1. 使用场景
当页面的数据不需要经常更改时可采用静态页面方式。
2. 使用静态页面的好处
(1)提高网站的访问速度
(2)减轻服务器负担
(3)利于搜索引擎抓取
3. ASP.NET生成静态页面
生成静态页面方法有很多种,先说下我使用的其中的一种。参考资料
基本思路:
(1)创建模板template.html文件,在里面定义一些特殊的字符串格式用于替换内容,如$htmlformat
(2)读取模板,赋值到StringBuilder对象中
(3)将特殊的字符串格式替换为你想要的内容
(4)创建新的静态页面,并将StringBuilder对象写入到文件中即可
4. 方法
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.IO; /// <summary> ///ConvertHtmlPage 生成静态页面 /// </summary> public class ConvertHtmlPage { /// <summary> /// 生成HTML文件 /// </summary> /// <param name="templatePath">模板路径</param> /// <param name="templateName">模板名称</param> /// <param name="htmlPath">生成HTML的路径</param> /// <param name="htmlName">生成HTML的名称</param> /// <param name="format">替换的内容</param> /// <returns></returns> public static bool CreatePage(string templatePath,string templateName, string htmlPath, string htmlName,List<string> format) { try { //读取模板文件 StringBuilder htmltext = new StringBuilder(); using (StreamReader sr = new StreamReader(templatePath+templateName)) { string line; while ((line = sr.ReadLine()) != null) { htmltext.AppendLine(line); } sr.Close(); } //替换HTML中的标记内容 for (int i = 0; i < format.Count; i++) { htmltext.Replace("$htmlformat[" + i + "]", format[i]); } //生成HTML文件 using (StreamWriter sw = new StreamWriter(htmlPath+htmlName, false, System.Text.Encoding.GetEncoding("GB2312"))) { sw.WriteLine(htmltext); sw.Flush(); sw.Close(); } } catch (Exception ex) { return false; } return true; } }
附:DEMO实例点击此处本站下载。
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】