C#动态生成html页
Html生成模块:WriteHtml.cs
1 using System.Collections.Generic;2 using System.IO;3 using System.Text;4 5 namespace System6 {7 /// <summary>8 /// Html9 /// </summary>
10 public class Html
11 {
12 /// <summary>
13 /// 生成Html
14 /// </summary>
15 /// <param name="template">模版文件</param>
16 /// <param name="path">生成的文件目录</param>
17 /// <param name="htmlname">生成的文件名</param>
18 /// <param name="dic">字典</param>
19 /// <param name="message">异常消息</param>
20 /// <returns></returns>
21 public bool Create(string template, string path, string htmlname, Dictionary<string, string> dic, ref string message)
22 {
23 bool result = false;
24 string templatepath = System.Web.HttpContext.Current.Server.MapPath(template);
25 string htmlpath = System.Web.HttpContext.Current.Server.MapPath(path);
26 string htmlnamepath = Path.Combine(htmlpath, htmlname);
27 Encoding encode = Encoding.UTF8;
28 StringBuilder html = new StringBuilder();
29
30 try
31 {
32 //读取模版
33 html.Append(File.ReadAllText(templatepath, encode));
34 }
35 catch (FileNotFoundException ex)
36 {
37 message = ex.Message;
38 return false;
39 }
40
41 foreach (KeyValuePair<string,string> d in dic)
42 {
43 //替换数据
44 html.Replace(
45 string.Format("${0}$", d.Key),
46 d.Value);
47 }
48
49 try
50 {
51 //写入html文件
52 if (!Directory.Exists(htmlpath))
53 Directory.CreateDirectory(htmlpath);
54 File.WriteAllText(htmlnamepath, html.ToString(), encode);
55 result = true;
56 }
57 catch (IOException ex)
58 {
59 message = ex.Message;
60 return false;
61 }
62
63 return result;
64 }
65 }
66 }
模版文件:/Template/a.html
1 <!DOCTYPE html>2 <html xmlns="http://www.w3.org/1999/xhtml">3 <head>4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>5 <title>$title$</title>6 </head>7 <body>8 $content$<br/>9 $author$
10 </body>
11 </html>
调用网页:test.ashx
1 using System;2 using System.Collections.Generic;3 using System.Web;4 5 namespace Wycz6 {7 /// <summary>8 /// test 的摘要说明9 /// </summary>
10 public class test : IHttpHandler
11 {
12
13 public void ProcessRequest(HttpContext context)
14 {
15 //context.Response.ContentType = "text/plain";
16 //context.Response.Write("Hello World");
17 string template = "/Template/a.html";
18 string path = "/test/";
19 string htmlname = "a.html";
20 Dictionary<string, string> dic = new Dictionary<string, string>();
21 Html h = new Html();
22 string message = string.Empty;
23
24 dic.Add("title", "动态生成html");
25 dic.Add("content", "测试内容");
26 dic.Add("author", "P.R");
27
28 if (!h.Create(template, path, htmlname, dic, ref message))
29 {
30 context.Response.Write("出错啦:<br/>");
31 context.Response.Write(message);
32 context.Response.End();
33 }
34
35 context.Response.Redirect(path + htmlname);
36 }
37
38 public bool IsReusable
39 {
40 get
41 {
42 return false;
43 }
44 }
45 }
46 }
效果图:
C#动态生成html页相关推荐
- layui动态生成多页签
layout.html <!DOCTYPE html> <html> <head><meta charset="utf-8">< ...
- Asp.net动态生成html页面
作者:网际浪子专栏(曾用名littlehb) http://blog.csdn.net/littlehb/ 适用于:Microsoft ASP.NET 摘要:asp.net动态生成html页面,适用 ...
- 动态生成HTML页面
查看文章 Asp.net动态生成html页面2007-06-17 09:51此功能适用于后台数据库功能不强的web站点,即大部分文本不是存放在数据库的记录中,而是放在html文件或者xml文件 ...
- 织梦dedecms会员中心投稿,管理员审核后,文档稿件动态浏览,禁止生成静态页
后台模型管理中,会员稿件默认状态,官方默认只有三个选项.如果选择投稿后未审核,那么.后台审核后,文档会自动生成静态页.没法动态浏览.解决办法是增加一个选项.如图 需要修改三个文件 一是,后台模板目录中 ...
- awstats CGI模式下动态生成页面缓慢的改进
本文可以看做是 多server多站点情况下awstats日志分析 这篇文章的下篇,在使用过程中发现awstats在cgi模式下动态生成分析报告慢的问题 (尤其是有些站点每天两个多G的日志,查看起来简直 ...
- 给gridview动态生成radiobutton添加OnCheckedChanged监听函数
在写研究生系统审核功能的时候,有一个页面的gridview动态生成四列radiobutton,在gridview控件外有四个radiobutton分别控制这四列的全选中.那么当外面的某个全选按钮选中后 ...
- bootstrap自动生成html,Bootstrap框架动态生成Web页面文章内目录的方法
引言在写博客的时候,为了条理性起见,经常会使用许多小标题,当文章长时,需要来回在不同的标题之间穿梭,如果手动添加目录,添加锚点,实在是麻烦,为此,可以动态生成一块目录区域,并使用Bootstrap提供 ...
- ext2.2打造全新功能grid系列--仅仅动态生成GridPanel
发现Ext中文官网论坛上一些Ext的好文章,真是相见恨晚啊,比自己看Document要快十倍,extit 简直是太伟大了.一点一点的学习,一点一点的摘抄到我的Blog,纯属收藏,急于看别的内容的朋友, ...
- 用Aspose.Words for .NET动态生成word文档中的图片或水印
1.概述 在项目中生成word文档,这个功能很普遍的,一般生成都是纯文字或是列表的比较多,便于客户打印,而要把图片也生成到word文档中的需求有些客户也是需要的,例如产品图片.这次我们介绍的是如何利用 ...
最新文章
- 一文搞懂PointNet全家桶——强势的点云处理神经网络
- web前端技术杂谈--css篇(1)--浅谈margin(续)
- qa 芯片测试_芯片测试术语介绍CP、FT、WAT
- VTK:图片之ImageThreshold
- D3js(三):force实例
- 牛客小白月赛7 B 自杀游戏
- mysql 查询正在执行的事务以及锁 常用的sql语句
- [C++11]指针空值类型nullptr
- revit插件有哪些常用的?介绍几个常用的revit插件操作简单
- 有感于《成都私车数量超上海》
- 支付宝小程序的开通流程
- Symbian术语表
- 化工过程机械类毕业论文文献有哪些?
- RedHat(RHEL)6.2 X64 Oracle11g X64 安装参考文档
- python中sort什么意思,python中sort是什么意思
- java面试宝典2017
- linux 命令运行kodi,在Ubuntu/Debian/Raspbian中安装Kodi for Linux的方法
- 软件测试工资直追开发,是我的错觉吗?
- 云ERP来的正是时候!
- ionic3开发系列——ionic3懒加载配置
热门文章
- C++实现tree234树(附完整源码)
- C语言二级指针内存模型建立
- 经典C语言程序100例之七九
- Ambari2.7.4+HDP3.1.4下sqoop增量导入只支持append模式,mysql直接进入hive的lastmodified的不支持。下面是增量的命令。
- 5、HIVE DML操作、load数据、update、Delete、Merge、where语句、基于分区的查询、HAVING子句、LIMIT子句、Group By语法、Hive 的Join操作等
- DBMS、hdfs、hive之间的数据转换之sqoop
- 03_Android NDK中C语言调用Java代码,javah的使用,javap的使用以及生成签名,Android.mk的编写,C代码的编写
- 2 TileMapObject的使用
- 服务器销售税率,转销售云服务器税率
- linux分区语言,Linux磁盘分区(9)-Go语言中文社区