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页的更多相关文章

  1. EasyUI中动态生成标签页

    这是最近学到的内容,当时是有思路但是不知道怎么获取当前的点击对象,就没有实现功能,通过更深入的学习,我知道了不仅仅是Java,Oracle中有一个this,同样的EasyUI中也存在一个this,来获 ...

  2. iTextSharp动态生成多页pdf及追加内容等记录

    1.要动态生成pdf,无非是用第三方或直接代码生成. 2.iTextSharp生成pdf问题点记录 dll相关下载 https://files.cnblogs.com/files/xlgwr/iTex ...

  3. 根据html页面模板动态生成html页面(c#类)

    本文转载自:http://www.cnblogs.com/yuanbao/archive/2008/01/06/1027985.html点击打开链接 一直以为动态生成静态页面不好做,昨天在网上找了下, ...

  4. 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版!

    转--http://www.2cto.com/kf/201402/277535.html 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版! 2014-02-11     ...

  5. 动态生成二维码并利用canvas合成出一张图片(类似海报、分享页)

    在前端开发并打算推广一个APP的时候,推广页是免不了的,而推广页的展示方式一般是给人家一个二维码,让别人自己去安装APP,这样前段任务也达到了,这次写这篇文章的原因主要还是总结一下,其中有很多不完善的 ...

  6. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  7. Dev Express 动态生成XRTable使用总结

    1. XRTableCell常见属性  XRTableCell xrTableCell = new XRTableCell(); A. 字体及字体大小 xrTableCell.Font = new S ...

  8. 用js动态生成css代码

    有时候我们需要利用js来动态生成页面上style标签中的css代码,方法很直接,就是直接创建一个style元素,然后设置style元素里面的css代码,最后把它插入到head元素中.但有些兼容性问题我 ...

  9. 使用dwr时动态生成table的一个小技巧

    这篇随笔是我在07年写的,因为当时用了自己建设的blog,后来停止使用了,今天看到备份数据库还在,恢复出来放到这里.留着记录用. 我在使用DWR时,试了很多次都无法在动态生成的table中的一个或多个 ...

随机推荐

  1. R爬虫知识点

    >>如何用 R 模仿浏览器的行为? GET / POST URLencode / URLdecode (破解中文網址的祕密) header & cookie 如何突破使用 cook ...

  2. <一>获取数据库连接

    一.JDBC_通过Driver接口获取数据库连接 1. Driver是一个接口:数据库厂商必须提供实现的接口,可以从其中 获取数据库连接. 2.JDBC URL由三部分组成,各部分用冒号隔开,格式:j ...

  3. HTML语法大全

      卷标 , 属性名称 , 简介  002 <! - - ... - -> 批注  003 <!> 跑马灯  004 <marquee>...</marque ...

  4. Matlab的标记分水岭分割算法

    1 综述 Separating touching objects in an image is one of the more difficult image processing operation ...

  5. Android课程---优化ListView列表视图(2)

    layout_simple.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  6. MySQL主主复制+MMM实现高可用

    架构图: 环境规划 角色 IP VIP 用途 DB1 192.168.56.101 192.168.56.111/113 主Master提供wirte+read DB2 192.168.56.102 ...

  7. vim正则表达式~转

    vim正则表达式(转) Vim中的正则表达式功能很强大,如果能自由运用,则可以完成很多难以想象的操作. 如果你比较熟悉Perl的正规表达式,可以直接参照与Perl正则表达式的区别一节. 一.使用正则表 ...

  8. 索引中include的魅力(具有包含性列的索引) (转)

    开文之前首先要讲讲几个概念 [覆盖查询] 当索引包含查询引用的所有列时,它通常称为“覆盖查询”. [索引覆盖] 如果返回的数据列就包含于索引的键值中,或者包含于索引的键值+聚集索引的键值中,那么就不会 ...

  9. How to inspect who is caller of func and who is the class of instance

    1. Who is the class of self instance ? class aa(object): def a(self): if self.__class__.__name__ == ...

  10. Python 开平方

    #!/user/bin/python3#files :using_sys.pyf = open("filename.txt","r+")num = f.read ...