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. 不可变字符串NSString

    /*字符串的常用方法*/ //1.通常用来把一些基本数据类型和字符串进行拼接 ; float b = 9527.0; NSString *string = [NSString stringWithFo ...

  2. Hadoop_UDAF示例

    UDAF: 多进一出 GenericUDAFEvaluator : 就是根据job的不同阶段执行不同的方法 Hive通过GenericUDAFEvaluator.Modle来确定job的执行阶段 PA ...

  3. MySQL每天自动增加分区

    有一个表tb_3a_huandan_detail,每天有300W左右的数据.查询太慢了,网上了解了一下,可以做表分区.由于数据较大,所以决定做定时任务每天执行存过自动进行分区. 1.在进行自动增加分区 ...

  4. JS全兼容检测浏览器类型及版本

    直接上代码: <script> var browser = (function () { var isIE6 = /msie 6/i.test(navigator.userAgent); ...

  5. 前端开发与SEO

    前端开发中通过一些小习惯,可以有利于SEO,本着蚊子再小也是肉的原则,能抓住自然不能忽略. 1.控制首页链接数量,不能过多,也不要太少.更不要为了凑数而添加 2.扁平化层次,力争跳转三次可以到任何页面 ...

  6. 别再为了iOS新系统设备而重新安装一个新版Xcode了.其实我们可以添加版本支持

    众所周知,Xcode7.3的代码补全是有问题的  如导入自定义类之后,在代码中并不会补全相应的类名... 但Xcode7.2是没有这个问题的,但很多时候我们自己的设备都升级到了iOS9.3.X系统,导 ...

  7. oauth基本流程和原理

    组装loginurl->去第三方登录验证->回调callbackurl+code(票据)->本地根据code+appid+appkey组装url隐式curl获取用户信息->完成 ...

  8. 【转】监听按钮除OnClick外其他事件的方法,附简易改编的UIButton类

    http://lib.csdn.net/article/unity3d/38463 作者:IceFantasyLcj 大家好,我是雨中祈雨.一直以来,CSDN都是我最好的编程助手.这是我在CSDN的第 ...

  9. 给Source Insight做个外挂系列之四--分析“Source Insight”

    外挂的目的就是将代码注入到其它进程中,所以必须要有目标进程才能完成注入,而所谓的目标进程通常是某软件的一部分或者是全部,所以要对目标程序有深入地了解.一般外挂都是针对某个应用程序开发的,其装载.运行都 ...

  10. Mac上安装django

    参考:https://docs.djangoproject.com/en/1.9/topics/install/#installing-official-release 升级pip sudo pip ...