C#动态生成html页
2013-07-15 10:11
218 查看
Html生成模块:WriteHtml.cs
模版文件:/Template/a.html
调用网页:test.ashx
效果图:
1 using System.Collections.Generic; 2 using System.IO; 3 using System.Text; 4 5 namespace System 6 { 7 /// <summary> 8 /// Html 9 /// </summary> 10 public class Html 11 { 12 /// <summary> 13 /// 生成Html 14 /// </summary> 15 /// <param name="template">模版文件</param> 16 /// <param name="path">生成的文件目录</param> 17 /// <param name="htmlname">生成的文件名</param> 18 /// <param name="dic">字典</param> 19 /// <param name="message">异常消息</param> 20 /// <returns></returns> 21 public bool Create(string template, string path, string htmlname, Dictionary<string, string> dic, ref string message) 22 { 23 bool result = false; 24 string templatepath = System.Web.HttpContext.Current.Server.MapPath(template); 25 string htmlpath = System.Web.HttpContext.Current.Server.MapPath(path); 26 string htmlnamepath = Path.Combine(htmlpath, htmlname); 27 Encoding encode = Encoding.UTF8; 28 StringBuilder html = new StringBuilder(); 29 30 try 31 { 32 //读取模版 33 html.Append(File.ReadAllText(templatepath, encode)); 34 } 35 catch (FileNotFoundException ex) 36 { 37 message = ex.Message; 38 return false; 39 } 40 41 foreach (KeyValuePair<string,string> d in dic) 42 { 43 //替换数据 44 html.Replace( 45 string.Format("${0}$", d.Key), 46 d.Value); 47 } 48 49 try 50 { 51 //写入html文件 52 if (!Directory.Exists(htmlpath)) 53 Directory.CreateDirectory(htmlpath); 54 File.WriteAllText(htmlnamepath, html.ToString(), encode); 55 result = true; 56 } 57 catch (IOException ex) 58 { 59 message = ex.Message; 60 return false; 61 } 62 63 return result; 64 } 65 } 66 }
模版文件:/Template/a.html
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title>$title$</title> 6 </head> 7 <body> 8 $content$<br/> 9 $author$ 10 </body> 11 </html>
调用网页:test.ashx
1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 5 namespace Wycz 6 { 7 /// <summary> 8 /// test 的摘要说明 9 /// </summary> 10 public class test : IHttpHandler 11 { 12 13 public void ProcessRequest(HttpContext context) 14 { 15 //context.Response.ContentType = "text/plain"; 16 //context.Response.Write("Hello World"); 17 string template = "/Template/a.html"; 18 string path = "/test/"; 19 string htmlname = "a.html"; 20 Dictionary<string, string> dic = new Dictionary<string, string>(); 21 Html h = new Html(); 22 string message = string.Empty; 23 24 dic.Add("title", "动态生成html"); 25 dic.Add("content", "测试内容"); 26 dic.Add("author", "P.R"); 27 28 if (!h.Create(template, path, htmlname, dic, ref message)) 29 { 30 context.Response.Write("出错啦:<br/>"); 31 context.Response.Write(message); 32 context.Response.End(); 33 } 34 35 context.Response.Redirect(path + htmlname); 36 } 37 38 public bool IsReusable 39 { 40 get 41 { 42 return false; 43 } 44 } 45 } 46 }
效果图:
相关文章推荐
- C#动态生成html页时候的问题及其解决方案
- C#动态生成html页
- C#动态生成Word文档并填充数据
- c#语言asp.net实现treeview控件读数据库动态生成树的代码
- C#获取动态生成的控件值
- C#之动态生成html文件
- [转]浅析C#编程实现动态生成Word文档_.Net教程
- [C#]在网页中动态的生成一个gif图片
- ArcGIS Server 动态生成缓存并替换瓦片(C#+AE)
- C#动态生成XML并在前台用javascript读取
- 秒懂C#通过Emit动态生成代码
- C#编程实现动态生成Word文档
- C#中自定义方法,动态编译,生成dll文件
- C# 动态生成类
- 用C#动态生成HTML的问题
- 用C#动态生成Word文档并将数据填入Word表格中
- C#动态生成树型结构的Web程序设计
- C#动态生成Word文档并填充数据
- [C#]对我的网页动态生成图片方法的修改
- (fujie724) C#:简单实现动态数据生成Word文档并保存