动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   .Net → 阅读文章

 asp.net生成静态页

作者:无从考证 来源:中国软件 
阅读 3161 人次 , 2006-2-15 14:26:00 

//生成HTML页
  public static bool WriteFile(string strText,string strContent,string strAuthor) 
  
{
   
string path = HttpContext.Current.Server.MapPath("/news/");
   Encoding code 
= Encoding.GetEncoding("gb2312");
   
// 读取模板文件
   string temp = HttpContext.Current.Server.MapPath("/news/text.html");
   StreamReader sr
=null;
   StreamWriter sw
=null;
   
string str="";  
   
try
   
{
    sr 
= new StreamReader(temp, code);
    str 
= sr.ReadToEnd(); // 读取文件
   }

   
catch(Exception exp)
   
{
    HttpContext.Current.Response.Write(exp.Message);
    HttpContext.Current.Response.End();
    sr.Close();
   }

  
   
   
string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
   
// 替换内容
   
// 这时,模板文件已经读入到名称为str的变量中了
   str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
   str = str.Replace("biaoti",strText);
   str 
= str.Replace("content",strContent);
   str 
= str.Replace("author",strAuthor);
   
// 写文件
   try
   
{
    sw 
= new StreamWriter(path + htmlfilename , false, code);
    sw.Write(str);
    sw.Flush();
   }

   
catch(Exception ex)
   
{
    HttpContext.Current.Response.Write(ex.Message);
    HttpContext.Current.Response.End();
   }

   
finally
   
{
    sw.Close();
   }

   
return true;

此函数放在Conn.CS基类中了
在添加新闻的代码中引用 注:工程名为Hover

    
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
    
{
     Response.Write(
"添加成功");
    }

    
else
    
{
     Response.Write(
"生成HTML出错!");
    }
 
 


模板页Text.html代码


  CODE:   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
  
<title>ShowArticle</title>
   
<body>
biaoti
<br>
content
<br>
author
</body>
</HTML>
biaoti
<br>
content
<br>
author
</body>
</HTML> 
  
 本文TagsC#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:顺序求出c(n,r)的排列组合
· 下一篇:根据数据库生成xml二法
· javamail在jsp中调用 二
· asp+发送Email完全手册
· CNET JSP BASIC: 安装JSP
· C#编程让Outlook乖乖交出帐户密码
· asp.net高级教程(三)---对象


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077