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中的一个或多个 ...
随机推荐
- URL重写无效
在IIS7或以上版本中使用urlrewriter时会出现无效的现像,这时候需要使用以下设置
- HDU 5833 Zhu and 772002(高斯消元)
题意:给n个数,从n个数中抽取x(x>=1)个数,这x个数相乘为完全平方数,求一共有多少种取法,结果模1000000007. 思路:每个数可以拆成素数相乘的形式,例如: x1 2=2^1 * 3 ...
- WPF整理-自定义一个扩展标记(custom markup extension)
"Markup extensions are used to extend the capabilities of XAML, by providing declarativeoperati ...
- css解决div的各种浏览器兼容性问题
方法一: min-height:500px;/*解决ie8.9.ff.chromet*/ height:100%;/*解决ie6.7*/ _height:500px;/*解决ie6超出自动溢出*/ 方 ...
- 本地测试SMTP服务器
一年前看计算机网络里的电子邮件协议的时候,想自己建一个本地SMTP服务器,然后用telnet发送消息,后来是没找到.这次又翻看了一下,找了个Windows平台下的hMailServer,挺方便的.步骤 ...
- Thinking Of Matrix
http://blog.163.com/bzm_square/blog/static/9355546320129582254842/ PS: 一种有关于矩阵的思维方法.....WiKi 向量空间,不定 ...
- 更换Mac记录
1. 重装Mac (1)格式化硬盘 (2)重装系统 (3)修改各种配置:电脑名.键盘等 2. 安装必要软件 (1)QQ.微信.网易云音乐等 (2)火狐.谷歌.SourceTree等 (3)安装Git ...
- js 获取滚动条的高度 以及 设置滚动条的高度
//设置窗口滚动条高度 function setScrollTop(top){ if(!isNaN(top))document.body.scrollTop = top; } //取窗口滚动条高度 f ...
- 《Linux及安全》实践2
<Linux及安全>实践2 [edited by 5216lwr] 一.Linux基本内核模块 1.1理解什么是内核模块 linux模块是一些可以作为独立程序来编译的函数和数据类型的集合. ...
- MAC emacs下安装php-mode
在某一个个页面(抱歉不记得了), 比较了三个配置,php-mode, php-mode-improved, xnhtml. 作者在比较后推荐xnhtml,但在xnthml官网下载链接一直显示403,真 ...