博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成静态页html
阅读量:4615 次
发布时间:2019-06-09

本文共 2259 字,大约阅读时间需要 7 分钟。

代码:

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    {        ///         /// html模板地址        ///         public string TemplateUrl { get; set; }        ///         /// 生成html保存地址        ///         public string DestinationUrl { get; set; }        ///         /// 要替换的键值对        ///         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();

 

转载于:https://www.cnblogs.com/xsj1989/p/7028604.html

你可能感兴趣的文章
Hdu 3395 【最大权值匹配】.cpp
查看>>
RPC框架
查看>>
DNS
查看>>
Winform项目部署
查看>>
React入门---开始前的准备(上)-2
查看>>
说说Kindle那些事
查看>>
HDOJ树形DP专题之Tree Cutting
查看>>
钢笔工具路径描边技巧 课时2:9描边路径的应用
查看>>
从0开始学习 GITHUB 系列之「初识 GITHUB」【转】
查看>>
循环控制
查看>>
分治策略
查看>>
回溯法总结
查看>>
ASP.NET MVC4 学习记录
查看>>
linux shell 语法学习
查看>>
清华大学-刘知远:表示学习与知识获取
查看>>
窗体的一些主要属性
查看>>
关于LCA的三种解法
查看>>
Pair Of Lines Codeforces 961D (随机)
查看>>
c语言--二维数组的首地址问题
查看>>
Python可视化:Seaborn库热力图使用进阶
查看>>