源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

ASP.NET编程简单实现生成静态页面的方法【附demo源码下载】

  • 时间:2021-09-11 16:24 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:ASP.NET编程简单实现生成静态页面的方法【附demo源码下载】
本文实例讲述了ASP.NET编程简单实现生成静态页面的方法。分享给大家供大家参考,具体如下: [b]1. 使用场景[/b] 当页面的数据不需要经常更改时可采用静态页面方式。 [b]2. 使用静态页面的好处[/b] (1)提高网站的访问速度 (2)减轻服务器负担 (3)利于搜索引擎抓取 [b]3. ASP.NET生成静态页面[/b] 生成静态页面方法有很多种,先说下我使用的其中的一种。参考资料 基本思路: (1)创建模板template.html文件,在里面定义一些特殊的字符串格式用于替换内容,如$htmlformat (2)读取模板,赋值到StringBuilder对象中 (3)将特殊的字符串格式替换为你想要的内容 (4)创建新的静态页面,并将StringBuilder对象写入到文件中即可 [b]4. 方法[/b]
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;
  }
}

[b]附:[/b]DEMO实例点击此处[url=http://xiazai.1sucai.cn/201707/yuanma/asp-dot-net-create-HTML-demo(1sucai.cn).rar][b]本站下载[/b][/url]。 更多关于asp.net相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/306.htm]asp.net文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/755.htm]asp.net操作json技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/692.htm]asp.net字符串操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/660.htm]asp.net操作XML技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/535.htm]asp.net ajax技巧总结专题[/url]》及《[url=http://www.1sucai.cn/Special/626.htm]asp.net缓存操作技巧总结[/url]》。 希望本文所述对大家asp.net程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部