日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

asp.net生成html靜態(tài)頁的多種方法

 wangn 2010-09-16
用C#做腳本的asp.net的方法,這個(gè)是我自己寫的,在《Visual C#.NET范例入門與提高》的P311,有對WebRequest和HttpRequest、HttpWebRequest、HttpWebResponse四個(gè)類的簡單說明
private bool CreateList(string url, string fna)
    {
        bool ok;
        //準(zhǔn)備生成
        string strHtml;
        StreamReader sr = null; //用來讀取流
        StreamWriter sw = null; //用來寫文件
        Encoding code = Encoding.GetEncoding("utf-8"); //定義編碼

        //構(gòu)造web請求,發(fā)送請求,獲取響應(yīng)
        WebRequest HttpWebRequest = null;
        WebResponse HttpWebResponse = null;
        HttpWebRequest = WebRequest.Create(url);
        HttpWebResponse = HttpWebRequest.GetResponse();

        //獲得流
        sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
        strHtml = sr.ReadToEnd();

        //寫入文件
        try
        {
            sw = new StreamWriter(fna, false, code);
            sw.Write(strHtml);
            sw.Flush();
            ok = true;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("<p>寫入文件出錯(cuò):" + ex.Message);
            HttpContext.Current.Response.End();
            ok = false;
        }
        finally
        {
            sw.Close();
        }
        return ok;
    }

調(diào)用的時(shí)候這樣:

//要生成html頁面的aspx頁面
        string url = @"http://localhost/list.aspx";
        //html頁面文件名
        string fna = Server.MapPath("") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";
        if (CreateList(url, fna))
        {
            Response.Write("<p>生成文件成功:" + fna);
        }

第二種方法是用一個(gè)html模板生成一個(gè)html頁面,模版里面有對應(yīng)的標(biāo)簽,可以從數(shù)據(jù)庫和別的地方取數(shù)據(jù),填寫這個(gè)標(biāo)簽,生成一個(gè)html頁面,這個(gè)方法在很多新聞系統(tǒng)里有用到

我參考這里面的代碼寫的:http://www./web/net/201/065118272748882.html

private string CreateDetailPage(string EventID,string EventTitle, string EventBody, string EventTime, string EventStat)
    {
        //模版所有路徑、模版文件名、生成的文件路徑、生成的文件名
        string path, temp, htmlfilepath,htmlfilename;
        path = Server.MapPath("");
        temp = Server.MapPath("testhtml.htm");
        htmlfilepath = path;
        htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";

        //讀模版
        Encoding code = Encoding.GetEncoding("gb2312");

        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";

        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd(); // 讀取文件
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write("<p>讀取文件出錯(cuò):" + exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }

        // 替換內(nèi)容
        // 對應(yīng)模版里的設(shè)置要修改
        str = str.Replace("re_symbol_EventID", EventID);
        str = str.Replace("re_symbol_EventTitle", EventTitle);
        str = str.Replace("re_symbol_EventBody", EventBody);
        str = str.Replace("re_symbol_EventTime", EventTime);
        str = str.Replace("re_symbol_EventStat", EventStat);

        // 寫文件
        try
        {
            sw = new StreamWriter(htmlfilepath + "\\" + htmlfilename, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("<p>寫入文件出錯(cuò):" + ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
        return htmlfilename;
    }

調(diào)用的時(shí)候這樣:

//取內(nèi)容,這里我取了頁面上的一個(gè)gridview里的選中行的數(shù)據(jù)
        int i;
        i = GridView1.SelectedIndex;
        if (i == null || i==-1) i = 0;
        string EventID, EventTitle, EventBody, EventTime, EventStat;
        EventID=GridView1.Rows[i].Cells[0].Text;
        EventTitle=GridView1.Rows[i].Cells[1].Text;
        EventBody=GridView1.Rows[i].Cells[2].Text;
        EventTime=GridView1.Rows[i].Cells[3].Text;
        EventStat=GridView1.Rows[i].Cells[4].Text;
       
        //生成文件,返回文件名
        string fna;
        fna=CreateDetailPage(EventID, EventTitle, EventBody, EventTime, EventStat);
        Response.Write("<p>生成文件成功:" + fna);

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多