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中的一个或多个 ...
随机推荐
- PHP中面向对象的关键字
php面向对象中常用的关键字有final.static.const (1)final: 1,final不能修饰成员属性 2,final只能修饰类和方法 作用: 使用final修饰的类不能被子类继承 使 ...
- IBatis按条件分页查询
XML中代码 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC & ...
- spark 2.0 Vector toBreeze
def toBreeze( _v : Vector ) : BZV[Double] = { _v match { case x : org.apache.spark.mllib.linalg.Dens ...
- jQuery实现的简单小功能(实用)
1.返回顶部使用JQuery的animate和scrollTop方法可以创建简单地返回顶部的动画: // Back to top $('#top').click(function (e) { e.pr ...
- 通过IP地址屏蔽各种“推广”
事情的起因是这样的:最近老是发现iPhone应用的底部出现各种横条广告,一开始以为是Google的广告推广,所以没管它,但是最近这些广告越来越猖狂,里面的内容越来越垃圾.今天仔细一看,原来不是Goog ...
- 如何在eclipse将程序导出成jar文件
如何在eclipse将程序导出成jar文件 听语音 | 浏览:916 | 更新:2015-08-22 17:57 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师傅为你的电脑系统,选个靠谱师傅 ...
- 微信 {"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"}
{"errcode":,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]" ...
- Sharepoint client model 中出现Cannot invoke HTTP DAV request. There is a pending query 的解决办法
由于近期在某项目中使用sharepoint client 对象模型做项目 在sharepoint 2010环境下正常,但迁移到sharepoint 2013后报错,提示如下 Cannot invoke ...
- 真机测试及布署Code Sign error问题总结
Code Sign error: Certificate identity 'iPhone Developer: idf (XR9HN3TD7E)' appears more than once in ...
- 《Linux内核分析》期末总结
Linux内核设计期中总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 前八周博客汇总及总结 Linux内核设计第一周——从汇编语言出发理解计算机工作原理 我们学习了汇编语言的基础知识,这一 ...