zoukankan      html  css  js  c++  java
  • C#动态生成html页

    Html生成模块:WriteHtml.cs

    复制代码
     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 }
    复制代码

    效果图:

  • 相关阅读:
    领域驱动设计概念(Domain-driven Design), Flower(响应式微服务框架)
    主流RPC框架通讯协议实现原理与源码解析
    响应式微服务框架Flower——快速上手
    netty源码-server端绑定端口流程
    ubuntu 20.04版本更新软件源为国内源(清华、网易、阿里云等等)
    ubuntu20.04源码安装nginx
    docker环境下Java获取cpu核心数不准确,实际上是宿主机的cpu核心数
    利用docker快速搭建创建开发环境
    mac配置python环境
    Apache Maven-创建项目
  • 原文地址:https://www.cnblogs.com/soundcode/p/3190655.html
Copyright © 2011-2022 走看看