网站生成静态页实现方法

使用模板生成静态页面....

很原始的写法,不知道大家有没有很好的解决方案?

 模板是网上下载的...

1 .模板页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title></title>
    
<link rel="stylesheet" type="text/css" href="Content.css" />
</head>
<body>
    
<div id="content">
        
<div id="search">
            
<form action="" method="get">
            
<p>
                搜索:
&nbsp;<input type="text" /><input type="image" src="http://www.cnblogs.com/../images/zoom.jpg" value="Submit" alt="Search" title="Search" />
            
</p>
            
</form>
        
</div>
        
<div id="header">
            
<h1>
                CONTENT_QBNAME
</h1>
            
<p>
                CONTENT_QBPROPERTY
</p>
        
</div>
        
<div id="body">
            
<div id="links">
                
<p>
                    
<font style=" font-size:large; font-weight:bold">&nbsp;&nbsp;tags标签</font>
                
</p>
                
<p>
                    CONTENT_TAGS
                
</p>
                
<p>
                    
<font style=" font-size:large; font-weight:bold">&nbsp;&nbsp;相关信息</font>
                
</p>
                
<p>
                    CONTENT_RELATION
                
</p>
            
</div>
            
<div id="main">
                
<h2>
                    内容:
</h2>
                
<p>
                    CONTENT_TEXT
                
</p>
                
<p>
                
&nbsp;
                
</p>
            
</div>
        
</div>
        
<div id="footer">
            Copyright © 2011 sobne.cn. All Rights Reserved.
        
</div>
    
</div>
</body>

</html>  


 css文件内容:

 

body {    padding                    : 0;
    margin                    
: 0;
    background                
: #FFFFFF;
    color                    
: #000000;
    font-family                
: arial, verdana, sans-serif;
    font-size                
: 85%
}


#content 
{
    width                    
: 800px;
    margin-left                
: auto;
    margin-right            
: auto;
    border                    
: 0px solid;
}

#header 
{
    margin                    
: 5px;
    padding-left            
: 5px;    
    width                    
: 600px;
    color                    
: #cc0000;
    background                
: #FFFFFF;
}

/* IE Hack */

* html #header 
{
    margin                    
: -60px 5px 5px 5px;
    padding-left            
: 5px;    
    width                    
: 575px;
    color                    
: #cc0000;
    background                
: #FFFFFF;
    
}

#header p 
{
    margin-top                
: -19px;
    margin-left                
: 2px;
    font-size                
: 90%;
    color                    
: silver;
    background                
: #FFFFFF;
}

#header input 
{
    margin-top                
: -17px; 
    text-align                
: right;    
}

#search 
{
    width                    
: 210px;
    margin-top                
: 2px;
    margin-left                
: 160px;
    float                    
: right;
    font-size                
: 80%;
    color                    
: silver;
    background                
: #FFFFFF;    
}

/* IE Hack */

* html #search 
{
    width                    
: 210px;
    margin-top                
: 27px;
    margin-left                
: 160px;
    float                    
: right;
    font-size                
: 80%;
    color                    
: silver;
    background                
: #FFFFFF;    
}

#body 
{
    border-top                
: 1px solid silver;
}

#body a 
{
    color                    
: #cc0000;
    background                
: #FFFFFF;
    text-decoration            
: none;
}

#footer 
{
    text-align                
: right;
    font-size                
: 75%;
    color                    
: silver;
    background                
: #FFFFFF;
    border-top                
: 1px solid silver;
}

#footer a 
{
    color                    
: silver;
    background                
: #FFFFFF;
    text-decoration            
: none;
}

#links 
{
    float                    
: left;
    width                    
: 150px;
    margin                    
: 5px;

}

#links a 
{
    color                    
: #cc0000;
    background                
: #FFFFFF;
    text-decoration            
: none;
    font-size                
: 120%;
}
#links p 
{
    color                    
: silver;
    background                
: #FFFFFF;
    font-size                
: 85%;
}

#main 
{
    margin                    
: 5px 5px 5px 160px;
    padding                    
: 15px;
    border-left                
: 1px solid silver;
}

2.生成代码

 仅写核心代码:

        string HTMLPath = HttpContext.Current.Server.MapPath("~//HTML/");
        
string ContentModel = HttpContext.Current.Server.MapPath("Content.htm");


        
string HtmlFilename ="test.html";
        
//写入业务表
        string path = DateTime.Today.Year.ToString() + "_" + DateTime.Today.Month.ToString() + "\\";
        
        
//写静态页
        HTMLPath = HTMLPath + path;
        
if (!Directory.Exists(HTMLPath))
            Directory.CreateDirectory(HTMLPath);
        Encoding encoding 
= Encoding.GetEncoding("gb2312");
        
// 读取模板文件
        StreamReader sr = null;
        StreamWriter sw 
= null;
        
string str = "";
        
try
        {
            sr 
= new StreamReader(ContentModel, encoding);
            str 
= sr.ReadToEnd(); // 读取文件
        }
        
catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }

        
// 替换内容
        str = str.Replace("CONTENT_QBNAME""");
        str 
= str.Replace("CONTENT_QBPROPERTY""");
        str 
= str.Replace("CONTENT_TAGS""");
        str 
= str.Replace("CONTENT_RELATION""");
        str 
= str.Replace("CONTENT_TEXT""");
        
// 写文件
        try
        {
            sw 
= new StreamWriter(HTMLPath + HtmlFilename, false, encoding);
            sw.Write(str);
            sw.Flush();
        }
        
catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        
finally
        {
            sw.Close();
        }

 

posted @ 2011-03-04 09:39  undefined?  阅读(799)  评论(4编辑  收藏  举报