1. 程式人生 > >asp.net怎樣一鍵生成靜態頁面,例項說明

asp.net怎樣一鍵生成靜態頁面,例項說明

二,根據對應模版,靜態出一個include頁面

ok,好吧,其實我這裡取巧了,先跳過,後面總結裡再詳說。
為了使本例簡單,現在先假設模版頁已經生成了html格式。程式碼如下
<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{$title} - {$classcName} - <!--#include file="/include/common/siteTitle.html" --></title>
    <meta name="description" content="{$seoDescription},<!--#include file="/include/common/siteDescription.html" -->" />
    <meta name="keywords" content="{$seoKeyWords},<!--#include file="/include/common/siteKeyWords.html" -->" />
    <link href="/css/layout.css" rel="stylesheet" type="text/css" />
    <link href="/css/layout_sub.css" rel="stylesheet" type="text/css" />
    <script src="/js/jquery/jquery.js" type="text/javascript"></script>
    <script src="/js/jquery/myJS.js" type="text/javascript"></script>
    <!--[if lte IE 6]>
    <script type="text/javascript" src="/js/ie6png.js"></script>
    <![endif]-->
    <script type="text/javascript">
        $(function () {
            $(".hotSchool .tabs").tabs(".hotSchool div.contentC", { event: 'click', effect: 'fade' });
        });
    </script>
</head>
<body>
    <div id="bgImgWrap">
        <img src="/images/back1.jpg" alt="" />
    </div>
    <!--#include file="/include/common/picStory.html" -->
    <div class="bigWrap">
        <div class="sideWrapBg png"></div>
        <div class="sideWrap png">
            <div class="logo noTxt clearfix"></div>
            <!--#include file="/include/common/sideNav.html" -->
        </div>
        <div class="mainWrap png">
            <!-- top search /-->
            <!--#include file="/include/common/head.html" -->

            <!-- 頻道頁頂部導航 /-->
            <div class="cWrap subTop clearfix">
                <div class="left subTitle">{$channelname}</div>
                <div class="left subNav">
                    <!--{$subNav}-->
                </div>
            </div>

            <!-- sub wrap -->
            <div class="cWrap subWrap pie png clearfix">
                <!-- 文章詳細 /-->
                <div class="detailWrap newsListWrap newsDetailWrap clearfix">
                    <div class="subNavWrap clearfix">
                        <div class="subNav left">{$locationget}</div>
                    </div>
                    <h1>{$title}</h1>
                    <div class="infoWrap clearfix">
                        <div class="shareWrap"> 
                            <div id="bdshare" class="bdshare_t bds_tools get-codes-bdshare">
                                <span class="left">分享本頁:</span>
                                <a class="bds_qzone"></a>
                                <a class="bds_tsina"></a>
                                <a class="bds_tqq"></a>
                                <a class="bds_renren"></a>
                                <a class="bds_douban"></a>
                                <span class="bds_more"></span>
                            </div>
                        </div>
                        <div class="dateWrap">發表:{$addDate} </div>
                        <div class="readWrap">閱讀:<span id="viewNums">...</span></div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="subHR clearfix png"></div>
                    <!-- Intro /-->
                    <div class="introWrap clearfix">
                         {$intro}
                    </div>
                    <!-- 正文 /-->
                    <div class="txtWrap clearfix minArticle">
                        {$content}
                    </div>


                    <!-- 相關文章 /-->
                    <div>
                    </div>
                </div>
            </div>
            <!--/ sub wrap -->

            <!-- footer /-->
            <!--#include file="/include/common/foot.html" -->
        </div>
    </div>
</body>
</html>
ok,現在我們把這個頁面命名為“CS1.html”,放在根目錄下的“Template”資料夾下,那麼,我們建一個“include.aspx”頁面,作使用者輸入文章用,那麼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="include.aspx.cs" validateRequest="false" Inherits="Admin_CMS_include" %>
<!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 runat="server">
    <title>文章輸入頁面</title>
</head>
<body>
    <form id="form1" runat="server">  
    <div>
        文章標題:<asp:TextBox ID="txtTitle" runat="server" Height="26px" Width="350px"></asp:TextBox><br />    
        文章摘要:<asp:TextBox ID="txtIntro" runat="server" Height="26px" Width="350px"></asp:TextBox><br />    
        文章內容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine" Width="350px"></asp:TextBox><br />  
        <br />  
        <asp:Button ID="btnInclude" runat="server" OnClick="btnInclude_Click" Text="生成靜態頁" />  
    </div>  
    </form>  
</body>
</html>
aspx.cs頁面程式碼如下,記得引用
using System.IO;
using System.Text;
那麼,主要程式碼如下:
  protected void btnInclude_Click(object sender, EventArgs e)
    {
        //other code;
        //Get date into dataSource;    

        string mbPath = Server.MapPath("~/Web/Template/CS1.html");
        Encoding code = Encoding.GetEncoding("UTF-8");
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = null;    
        try
        {
            sr = new StreamReader(mbPath, code);
            str = sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            sr.Close();
        }
        string fileName = DateTime.Now.ToString("MMddHHmm") + ".html";//儲存靜態頁面的副檔名

        //整章的關鍵點,替換掉相應字串 
        str = str.Replace("{$Title}", txtTitle.Text);
        str = str.Replace("{$intro}", txtIntro.Text);
        str = str.Replace("{$content}", txtContent.Text);  

        //生成靜態檔案    
        try
        {
            sw = new StreamWriter(Server.MapPath("~/html/") + fileName, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            sw.Close();
            Response.Write("<a href=/Web/html/" + fileName + " mce_href=" + fileName + " target=_blank>" + fileName + "</a>已經生成!");
        }
    }