代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks; namespace Framework.Common
{
public class StaticPage
{
/// <summary>
/// html模板地址
/// </summary>
public string TemplateUrl { get; set; }
/// <summary>
/// 生成html保存地址
/// </summary>
public string DestinationUrl { get; set; }
/// <summary>
/// 要替换的键值对
/// </summary>
public Hashtable KValues { get; set; } public StaticPage() { }
public StaticPage(string templateUrl, string destinationUrl, Hashtable kValues)
{
this.TemplateUrl = templateUrl;
this.DestinationUrl = destinationUrl;
this.KValues = kValues;
} public bool Save()
{
bool _isOk = false;
WebResponse response = WebRequest.Create(TemplateUrl).GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
{
string tempContent = reader.ReadToEnd();
foreach (string item in KValues.Keys)
{
tempContent = tempContent.Replace("{" + item.ToString() + "}", KValues[item].ToString());
}
using (StreamWriter write = new StreamWriter(DestinationUrl, false, System.Text.Encoding.UTF8))
{
write.Write(tempContent);
write.Flush();
write.Close();
_isOk = true;
}
}
return _isOk;
}
}
}

使用:

Models.OfficialStoryInfo item = OfficialStoryInfoBLL.GetEntity(id);
string tempUrl = HttpContext.Current.Server.MapPath("~/temp/officialstory.txt");
string destUrl = HttpContext.Current.Server.MapPath("~/html/OfficialStory/" + item.ID + ".html");
System.Collections.Hashtable ht = new System.Collections.Hashtable();
ht.Add("title", item.Name);
ht.Add("contents", item.Description);
bool isok = new Common.StaticPage() { DestinationUrl = destUrl, KValues = ht, TemplateUrl = tempUrl }.Save();

生成静态页html的更多相关文章

  1. ASP.NET MVC 解析模板生成静态页一(RazorEngine)

    简述 Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.在早期的MVC版本中默认使用的是ASPX模板引擎,Razor在语法上的确不错,用起来非常方便,简洁的语法 ...

  2. 基于PHP生成静态页的实现方法

    t1.php 复制代码 代码如下: <?php// 方法一根据模版生成静态页面// replaceTemplateString函数用于替换模板中指定字符串function replaceTemp ...

  3. 生成静态页面的PHP类

    生成静态页面的PHP类: 复制代码代码如下: <?php   class html   {    var $dir; //dir for the htmls(without/)    var $ ...

  4. mvc分页生成静态页,mvc生成静态页

    http://blog.csdn.net/xxj_jing/article/details/7899125 分页生成静态页 http://www.cnblogs.com/luanyilin/archi ...

  5. C# 用模板生成静态页

    最近在研究静态页输出的问题,找了一些资料.做了一个简单的模板模式的静态输出 模板代码: <html xmlns="http://www.w3.org/1999/xhtml"& ...

  6. NET MVC RazorEngine 解析模板生成静态页

    ASP.NET MVC 解析模板生成静态页一(RazorEngine) 简述 Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.在早期的MVC版本中默认使用的是 ...

  7. tp 生成静态页

    $this->fetch()返回的是html 可以直接写入到HTML文件内生成静态页

  8. dedesmc 手机端生成静态页

    dedesmc 手机端生成静态页 1.首先下载插件,下载地址:https://pan.baidu.com/s/1Nfx_KBYuxRkZ7VzoPxy28g 密码:83x7 2.进入 dedecms ...

  9. ThinkPHP生成静态页buildHtml方法

    原来ThinkPHP自带了生成静态页的函数buildHtml,使用起来很方便!最新的手册里没写这个方法,向大家介绍一下. PHP 1 2 3 4 5 6 7 8 9 10 11     protect ...

  10. ASP.NET MVC 利用Razor引擎生成静态页

    实现原理及步骤: 1.通过ViewEngines.Engines.FindView查找到对应的视图,如果是部分视图,则用:ViewEngines.Engines.FindPartialView: 2. ...

随机推荐

  1. Keras实现简单BP神经网络

    BP 神经网络的简单实现 from keras.models import Sequential #导入模型 from keras.layers.core import Dense #导入常用层 tr ...

  2. jenkins 构建一个maven项目

    1.首先在 全局工具配置 里配置maven的路径信息 这里因为之前已经下载了maven并放在了E盘,因此只需要在 MAVEN_HOME 添加maven文件夹的路径 如若本地还没maven,勾选 “自动 ...

  3. 生存分析与R

    生存分析与R 2018年05月19日 19:55:06 走在码农路上的医学狗 阅读数:4399更多 个人分类: R语言   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  4. c++官方文档-模版类

    #include <iostream> using namespace std; template<class T> class MyPair { private: T t[] ...

  5. 64位win10系统无法安装.Net framework3.5的两种解决方法

    参考网站: https://blog.csdn.net/zang141588761/article/details/52177290 在Windows10中,当我们安装某些软件的时候会提示“你的电脑上 ...

  6. leetcode748

    public class Solution { public string ShortestCompletingWord(string licensePlate, string[] words) { ...

  7. Android手机与服务器(案例一) webservice

    1.服务端 建立webservice,增加方法gettime和sum. 测试正常8080端口,关闭防火墙. Start启动服务,并且OpenBrowser,XE6真是方便啊 http://localh ...

  8. VB6 创建控制台应用程序

    ' 功能:为VB程序创建一个consolewindow.Private Declare Function AllocConsole Lib "kernel32" () As Lon ...

  9. mavenLocal默认地址转移

    maven的默认本地仓库为 USER_HOME/.m2/ windows开发我们大多不会讲本地仓库放在c盘下,而是重新指定了另一个存储位置. 在gradle中 使用 mavenLocal() 时的查找 ...

  10. Java使用poi从数据库读取数据生成Excel表格

    想要使用POI操作以xsl结尾的Excel,首先要下载poi相关的jar包,用到的jar有: poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.9.j ...