C#动态生成html页
Html生成模块:WriteHtml.cs
using System.Collections.Generic;
using System.IO;
using System.Text; namespace System
{
/// <summary>
/// Html
/// </summary>
public class Html
{
/// <summary>
/// 生成Html
/// </summary>
/// <param name="template">模版文件</param>
/// <param name="path">生成的文件目录</param>
/// <param name="htmlname">生成的文件名</param>
/// <param name="dic">字典</param>
/// <param name="message">异常消息</param>
/// <returns></returns>
public bool Create(string template, string path, string htmlname, Dictionary<string, string> dic, ref string message)
{
bool result = false;
string templatepath = System.Web.HttpContext.Current.Server.MapPath(template);
string htmlpath = System.Web.HttpContext.Current.Server.MapPath(path);
string htmlnamepath = Path.Combine(htmlpath, htmlname);
Encoding encode = Encoding.UTF8;
StringBuilder html = new StringBuilder(); try
{
//读取模版
html.Append(File.ReadAllText(templatepath, encode));
}
catch (FileNotFoundException ex)
{
message = ex.Message;
return false;
} foreach (KeyValuePair<string,string> d in dic)
{
//替换数据
html.Replace(
string.Format("${0}$", d.Key),
d.Value);
} try
{
//写入html文件
if (!Directory.Exists(htmlpath))
Directory.CreateDirectory(htmlpath);
File.WriteAllText(htmlnamepath, html.ToString(), encode);
result = true;
}
catch (IOException ex)
{
message = ex.Message;
return false;
} return result;
}
}
}
模版文件:/Template/a.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>$title$</title>
</head>
<body>
$content$<br/>
$author$
</body>
</html>
调用网页:test.ashx
using System;
using System.Collections.Generic;
using System.Web; namespace Wycz
{
/// <summary>
/// test 的摘要说明
/// </summary>
public class test : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string template = "/Template/a.html";
string path = "/test/";
string htmlname = "a.html";
Dictionary<string, string> dic = new Dictionary<string, string>();
Html h = new Html();
string message = string.Empty; dic.Add("title", "动态生成html");
dic.Add("content", "测试内容");
dic.Add("author", "P.R"); if (!h.Create(template, path, htmlname, dic, ref message))
{
context.Response.Write("出错啦:<br/>");
context.Response.Write(message);
context.Response.End();
} context.Response.Redirect(path + htmlname);
} public bool IsReusable
{
get
{
return false;
}
}
}
}
效果图:

C#动态生成html页的更多相关文章
- EasyUI中动态生成标签页
这是最近学到的内容,当时是有思路但是不知道怎么获取当前的点击对象,就没有实现功能,通过更深入的学习,我知道了不仅仅是Java,Oracle中有一个this,同样的EasyUI中也存在一个this,来获 ...
- iTextSharp动态生成多页pdf及追加内容等记录
1.要动态生成pdf,无非是用第三方或直接代码生成. 2.iTextSharp生成pdf问题点记录 dll相关下载 https://files.cnblogs.com/files/xlgwr/iTex ...
- 根据html页面模板动态生成html页面(c#类)
本文转载自:http://www.cnblogs.com/yuanbao/archive/2008/01/06/1027985.html点击打开链接 一直以为动态生成静态页面不好做,昨天在网上找了下, ...
- 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版!
转--http://www.2cto.com/kf/201402/277535.html 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版! 2014-02-11 ...
- 动态生成二维码并利用canvas合成出一张图片(类似海报、分享页)
在前端开发并打算推广一个APP的时候,推广页是免不了的,而推广页的展示方式一般是给人家一个二维码,让别人自己去安装APP,这样前段任务也达到了,这次写这篇文章的原因主要还是总结一下,其中有很多不完善的 ...
- 利用Java动态生成 PDF 文档
利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...
- Dev Express 动态生成XRTable使用总结
1. XRTableCell常见属性 XRTableCell xrTableCell = new XRTableCell(); A. 字体及字体大小 xrTableCell.Font = new S ...
- 用js动态生成css代码
有时候我们需要利用js来动态生成页面上style标签中的css代码,方法很直接,就是直接创建一个style元素,然后设置style元素里面的css代码,最后把它插入到head元素中.但有些兼容性问题我 ...
- 使用dwr时动态生成table的一个小技巧
这篇随笔是我在07年写的,因为当时用了自己建设的blog,后来停止使用了,今天看到备份数据库还在,恢复出来放到这里.留着记录用. 我在使用DWR时,试了很多次都无法在动态生成的table中的一个或多个 ...
随机推荐
- About_Web
成功网站的三要诀:内容.设计.营销 内容为王: 高质量的内容会促使网站走向成功.首先,用户有需求,他们需要被感动,被娱乐,被有料的内容和产品所吸引.漂亮的背景和亮骚的特效可能会有所助益,但终究只是辅助 ...
- Make github as your personal maven repository
前言: 开始用maven管理java项目后,突然发现自己写了一些通用的项目想要被别的项目依赖是件很麻烦的事.公司里项目依赖可以直接有maven仓库,但个人项目呢? github 再次显示其威力了,ex ...
- WPF整理-使用逻辑资源
"Traditional application resources consist of binary chunks of data, typically representing thi ...
- System memory,AGP memory和video memory【转】
system memory就是电脑的内存条上的,一般都很大.显卡不能访问 . video memory就是显示卡上的显存,一般是32,64,128M这样,速度最快,显卡可直接访问 .用来描述电脑上一 ...
- 使用非Web方式从CA申请证书
背景介绍:关于从CA申请证书这点事,网上的那些教程基本都是让我们通过访问https://server/certsrv这样的网页来操作的,我一直希望不依赖IIS就把这事干了,于是就有了下面的文章. 1. ...
- mycat的读写分离设置
官网:http://www.mycat.org.cn/ 一.jdk环境的安装1.安装jdk1.7,这是mycat推荐的jdk环境 2.之前是用yum安装的jdk1.6,首先查找下 #yum info ...
- linux添加环境变量(centos)
1.查看当前环境变量 #echo $PATH 2.增加环境变量 #vi /etc/profile export PATH=/usr/path/bin:$PATH 3.生效 #source /etc/p ...
- words
conscious[英][ˈkɒnʃəs][美][ˈkɑnʃəs]consensus[英][kənˈsensəs][美][kənˈsɛnsəs] scious sensuswaterflood; de ...
- 如何利用python监控主机存活并邮件、短信通知
功能: 1.使用定时任务执行脚本,检查主机存活信息2.将主机存活信息写入指定文件3.发现宕机的主机后给用户发邮件提醒备注:因为139邮箱在接受到邮件后会自动给用户发送条短信告知(且此服务免费),所以间 ...
- 数字和为sum的方法数
[编程题] 数字和为sum的方法数 给定一个有n个正整数的数组A和一个整数sum,求选择数组A中部分数字和为sum的方案数. 当两种选取方案有一个数字的下标不一样,我们就认为是不同的组成方案. 输入描 ...