博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.net生成htm静态文件的两种途径
阅读量:7027 次
发布时间:2019-06-28

本文共 2605 字,大约阅读时间需要 8 分钟。

现在很多网站特别是资讯类的都把内容生成静态页(htm\html\shtml等),这类总结了一下两种生成静态页的方法并做了一个Demo文件供大家下载。

分别是通过模板(比较常用)和根据url生成(不到万部则以不用,因为这中方式只能获取html的部分):
Asp.net生成静态文件(根据时间自动命名保持,默认扩展名是htm可以自行修改)。

通过收入内容替换模板或者url地址两种方式进行静态文件的生成

templete.htm为模板文件,htm为生成后的静态文件保存位置

这类粘贴出.cs文件

  1
None.gif  
//
51aspx.com生成静态页演示文件,转载请保留该信息
  2
None.gif     
public  partial 
class  _Default : System.Web.UI.Page
  3
ExpandedBlockStart.gif     {
  4
InBlock.gif        
protected 
void Page_Load(
object sender, EventArgs e)
  5
ExpandedSubBlockStart.gif        {
  6
InBlock.gif           
  7
ExpandedSubBlockEnd.gif        }
  8
InBlock.gif
  9
InBlock.gif        
//
根据模板生成,保持在html文件夹中(部分源码搜集于网络)
 10
InBlock.gif        
protected 
void Button1_Click(
object sender, EventArgs e)
 11
ExpandedSubBlockStart.gif        {
 12
InBlock.gif            
//
源码是替换掉模板中的特征字符
 13
InBlock.gif
 14
InBlock.gif            
string mbPath =Server.MapPath("template.htm");
 15
InBlock.gif            Encoding code = Encoding.GetEncoding("gb2312");
 16
InBlock.gif            StreamReader sr = 
null;
 17
InBlock.gif            StreamWriter sw = 
null;
 18
InBlock.gif            
string str = 
null;
 19
InBlock.gif
 20
InBlock.gif            
//
读取
 21
InBlock.gif            
try
 22
ExpandedSubBlockStart.gif            {
 23
InBlock.gif                sr = 
new StreamReader(mbPath, code);
 24
InBlock.gif                str = sr.ReadToEnd();
 25
InBlock.gif
 26
ExpandedSubBlockEnd.gif            }
 27
InBlock.gif            
catch (Exception ex)
 28
ExpandedSubBlockStart.gif            {
 29
InBlock.gif                
throw ex;
 30
ExpandedSubBlockEnd.gif            }
 31
InBlock.gif            
finally
 32
ExpandedSubBlockStart.gif            {
 33
InBlock.gif                sr.Close();
 34
ExpandedSubBlockEnd.gif            }
 35
InBlock.gif
 36
InBlock.gif            
//
根据时间自动重命名,扩展名也可以自行修改
 37
InBlock.gif            
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
 38
InBlock.gif            str = str.Replace("$title$", txtTitle.Text);
//
替换Title
 39
InBlock.gif            str = str.Replace("$content$", txtContent.Text);
//
替换content
 40
InBlock.gif
 41
InBlock.gif            
//
生成静态文件
 42
InBlock.gif            
try
 43
ExpandedSubBlockStart.gif            {
 44
InBlock.gif                sw = 
new StreamWriter(Server.MapPath("htm/") + fileName, 
false, code);
 45
InBlock.gif                sw.Write(str);
 46
InBlock.gif                sw.Flush();
 47
InBlock.gif
 48
ExpandedSubBlockEnd.gif            }
 49
InBlock.gif            
catch (Exception ex)
 50
ExpandedSubBlockStart.gif            {
 51
InBlock.gif                
throw ex;
 52
ExpandedSubBlockEnd.gif            }
 53
InBlock.gif            
finally
 54
ExpandedSubBlockStart.gif            {
 55
InBlock.gif                sw.Close();
 56
InBlock.gif                Response.Write("恭喜<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已经生成,保存在htm文件夹下!");
 57
ExpandedSubBlockEnd.gif            }
 58
InBlock.gif
 59
 60
ExpandedSubBlockEnd.gif        }
 61
InBlock.gif
 62
InBlock.gif
 63
InBlock.gif        
//
根据Url地址生成静态页保持
 64
InBlock.gif        
protected 
void Button2_Click(
object sender, EventArgs e)
 65
ExpandedSubBlockStart.gif        {
 66
InBlock.gif            Encoding code = Encoding.GetEncoding("utf-8");
 67
InBlock.gif            StreamReader sr = 
null;
 68
InBlock.gif            StreamWriter sw = 
null;
 69
InBlock.gif            
string str = 
null;
 70
InBlock.gif
 71
InBlock.gif            
//
读取远程路径
 72
InBlock.gif            WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
 73
InBlock.gif            WebResponse myTemp = temp.GetResponse();
 74
InBlock.gif            sr = 
new StreamReader(myTemp.GetResponseStream(), code);
 75
InBlock.gif            
//
读取
 76
InBlock.gif            
try
 77
ExpandedSubBlockStart.gif            {
 78
InBlock.gif                sr = 
new StreamReader(myTemp.GetResponseStream(), code);
 79
InBlock.gif                str = sr.ReadToEnd();
 80
InBlock.gif
 81
ExpandedSubBlockEnd.gif            }
 82
InBlock.gif            
catch (Exception ex)
 83
ExpandedSubBlockStart.gif            {
 84
InBlock.gif                
throw ex;
 85
ExpandedSubBlockEnd.gif            }
 86
InBlock.gif            
finally
 87
ExpandedSubBlockStart.gif            {
 88
InBlock.gif                sr.Close();
 89
ExpandedSubBlockEnd.gif            }
 90
InBlock.gif            
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
 91
InBlock.gif
 92
InBlock.gif            
//
写入
 93
InBlock.gif            
try
 94
ExpandedSubBlockStart.gif            {
 95
InBlock.gif                sw = 
new StreamWriter(Server.MapPath("htm/") + fileName, 
false, code);
 96
InBlock.gif                sw.Write(str);
 97
InBlock.gif                sw.Flush();
 98
InBlock.gif
 99
ExpandedSubBlockEnd.gif            }
100
InBlock.gif            
catch (Exception ex)
101
ExpandedSubBlockStart.gif            {
102
InBlock.gif                
throw ex;
103
ExpandedSubBlockEnd.gif            }
104
InBlock.gif            
finally
105
ExpandedSubBlockStart.gif            {
106
InBlock.gif                sw.Close();
107
InBlock.gif                Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
108
ExpandedSubBlockEnd.gif            }
109
InBlock.gif
110
ExpandedSubBlockEnd.gif        }
111
ExpandedBlockEnd.gif    }

只是一个Demo文件,仅供大家参考,也希望有其他生成方式的也讨论一下(部分源码搜集于网络)

本文转自 liudao 博客园博客,原文链接:http://www.cnblogs.com/liudao/archive/2007/05/16/748155.html,如需转载请自行联系原作者

你可能感兴趣的文章
磁盘及网络测试工具(iperf hdparm dd)
查看>>
MySQL深入10-利用Ameoba实现读写分离
查看>>
SCVMM2012R2 服务模版系列(二)包含Web应用程序的单层服务模版
查看>>
秋色园QBlog技术原理解析:性能优化篇:用户和文章计数器方案(十七)
查看>>
一个IE8 Bug的解决方法以及一些思考
查看>>
JCE安装
查看>>
base64 源码
查看>>
规划安装部署SharePoint Server 2007精解(上)
查看>>
Router Interface Configuration
查看>>
积少成多Flash(2) - ActionScript 3.0 基础之包、类、包外类、命名空间、属性、方法、接口和继承...
查看>>
Nginx基础之http、https、socket 、SSL及TCP简单关系
查看>>
云场景实践研究第83期:众安保险
查看>>
【mysql】一次有意思的数据库查询分析。
查看>>
CentOS 6.3_ RSync实现文件备份同步配置排除故障
查看>>
VDI序曲十七 RDVH虚拟主机中开启RemoteFX的硬件配置要求
查看>>
快速手动替换 Windows 7 系统字体
查看>>
用php调用graphviz轻松画拓扑图
查看>>
NA-NP-IE系列实验27: default-information originate
查看>>
UWA助力独立游戏开发!《江湖X:汉家江湖》性能诊断精讲!
查看>>
ASP中FSO的神奇功能
查看>>