小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

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

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

        //構(gòu)造web請(qǐng)求,發(fā)送請(qǐng)求,獲取響應(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頁(yè)面的aspx頁(yè)面
        string url = @"http://localhost/list.aspx";
        //html頁(yè)面文件名
        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è)面,模版里面有對(duì)應(yīng)的標(biāo)簽,可以從數(shù)據(jù)庫(kù)和別的地方取數(shù)據(jù),填寫這個(gè)標(biāo)簽,生成一個(gè)html頁(yè)面,這個(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)容
        // 對(duì)應(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)容,這里我取了頁(yè)面上的一個(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)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多