ASP.net(c#) 生成html的幾種解決方案[思路]第1/2頁(yè)
更新時(shí)間:2009年05月17日 00:51:56 作者:
下面的文章轉(zhuǎn)載自網(wǎng)絡(luò),其代碼很多都有問(wèn)題,這里只提供給大家一個(gè)思路.
方案1:
/// <summary >
/// 傳入U(xiǎn)RL返回網(wǎng)頁(yè)的html代碼
/// </summary >
/// <param name="Url" >URL </param >
/// <returns > </returns >
public static string getUrltoHtml(string Url)
{
errorMsg = "";
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
// Get the response instance.
System.Net.WebResponse wResp =wReq.GetResponse();
// Read an HTTP-specific property
//if (wResp.GetType() ==HttpWebResponse)
//{
//DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified;
//}
// Get the response stream.
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
return reader.ReadToEnd();
}
catch(System.Exception ex)
{
errorMsg = ex.Message ;
}
return "";
}
你可以用這個(gè)函數(shù)獲取網(wǎng)頁(yè)的客戶(hù)端的html代碼,然后保存到.html文件里就可以了。
方案2:
生成單個(gè)的靜態(tài)頁(yè)面不是難點(diǎn),難的是各個(gè)靜態(tài)頁(yè)面間的關(guān)聯(lián)和鏈接如何保持完整;特別是在頁(yè)面頻繁更新、修改、或刪除的情況下; 像阿里巴巴的頁(yè)面也全部是html的,估計(jì)用的是地址映射的功能
關(guān)于地址映射可參考:http://www.easewe.com/Article/ShowArticle.aspx?article=131
可以看看這個(gè)頁(yè)面,分析一下他的“競(jìng)價(jià)倒計(jì)時(shí)”功能
http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5
ASP.Net生成靜態(tài)HTML頁(yè)
在Asp中實(shí)現(xiàn)的生成靜態(tài)頁(yè)用到的FileSystemObject對(duì)象!
在.Net中涉及此類(lèi)操作的是System.IO
以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼
//生成HTML頁(yè)
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";
// 替換內(nèi)容
// 這時(shí),模板文件已經(jīng)讀入到名稱(chēng)為str的變量中了
str =str.Replace("ShowArticle",strText); //模板頁(yè)中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 寫(xiě)文件
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;
此函數(shù)放在Conn.CS基類(lèi)中了
在添加新聞的代碼中引用 注:工程名為Hover
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出錯(cuò)!");
}
模板頁(yè)Text.html代碼
<!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 >
提示添加成功后會(huì)出以當(dāng)前時(shí)間為文件名的html文件!上面只是把傳遞過(guò)來(lái)的幾個(gè)參數(shù)直接寫(xiě)入了HTML文件中,在實(shí)際應(yīng)用中需要先添加數(shù)據(jù)庫(kù),然后再寫(xiě)入HTML文件
方案3:
給一個(gè)客戶(hù)端參考的例子(SJ)
它的作用在于以客戶(hù)端的方式獲取某個(gè)頁(yè)面的代碼,然后可以做為其他用途,本例是直接輸出
<script >
var oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
oXmlHttp.open("GET","http://www.xrss.cn", false);
oXmlHttp.send()
var oStream = new ActiveXObject("ADODB.Stream");
if(oStream == null)
alert("您的機(jī)器不支持ADODB.Stream.")
else
{
oStream.Type=1;
oStream.Mode=3;
oStream.Open() ;
oStream.Write(oXmlHttp.responseBody);
oStream.Position= 0;
oStream.Type= 2;
oStream.Charset="gb2312";
var result= oStream.ReadText();
oStream.Close();
oStream = null;
var aa = window.open("","")
document.write(result);
aa.document.write(result);
}
</script >
復(fù)制代碼 代碼如下:
/// <summary >
/// 傳入U(xiǎn)RL返回網(wǎng)頁(yè)的html代碼
/// </summary >
/// <param name="Url" >URL </param >
/// <returns > </returns >
public static string getUrltoHtml(string Url)
{
errorMsg = "";
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
// Get the response instance.
System.Net.WebResponse wResp =wReq.GetResponse();
// Read an HTTP-specific property
//if (wResp.GetType() ==HttpWebResponse)
//{
//DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified;
//}
// Get the response stream.
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
return reader.ReadToEnd();
}
catch(System.Exception ex)
{
errorMsg = ex.Message ;
}
return "";
}
你可以用這個(gè)函數(shù)獲取網(wǎng)頁(yè)的客戶(hù)端的html代碼,然后保存到.html文件里就可以了。
方案2:
生成單個(gè)的靜態(tài)頁(yè)面不是難點(diǎn),難的是各個(gè)靜態(tài)頁(yè)面間的關(guān)聯(lián)和鏈接如何保持完整;特別是在頁(yè)面頻繁更新、修改、或刪除的情況下; 像阿里巴巴的頁(yè)面也全部是html的,估計(jì)用的是地址映射的功能
關(guān)于地址映射可參考:http://www.easewe.com/Article/ShowArticle.aspx?article=131
可以看看這個(gè)頁(yè)面,分析一下他的“競(jìng)價(jià)倒計(jì)時(shí)”功能
http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5
ASP.Net生成靜態(tài)HTML頁(yè)
在Asp中實(shí)現(xiàn)的生成靜態(tài)頁(yè)用到的FileSystemObject對(duì)象!
在.Net中涉及此類(lèi)操作的是System.IO
以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼
復(fù)制代碼 代碼如下:
//生成HTML頁(yè)
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";
// 替換內(nèi)容
// 這時(shí),模板文件已經(jīng)讀入到名稱(chēng)為str的變量中了
str =str.Replace("ShowArticle",strText); //模板頁(yè)中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 寫(xiě)文件
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;
此函數(shù)放在Conn.CS基類(lèi)中了
在添加新聞的代碼中引用 注:工程名為Hover
復(fù)制代碼 代碼如下:
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出錯(cuò)!");
}
模板頁(yè)Text.html代碼
復(fù)制代碼 代碼如下:
<!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 >
提示添加成功后會(huì)出以當(dāng)前時(shí)間為文件名的html文件!上面只是把傳遞過(guò)來(lái)的幾個(gè)參數(shù)直接寫(xiě)入了HTML文件中,在實(shí)際應(yīng)用中需要先添加數(shù)據(jù)庫(kù),然后再寫(xiě)入HTML文件
方案3:
給一個(gè)客戶(hù)端參考的例子(SJ)
它的作用在于以客戶(hù)端的方式獲取某個(gè)頁(yè)面的代碼,然后可以做為其他用途,本例是直接輸出
復(fù)制代碼 代碼如下:
<script >
var oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
oXmlHttp.open("GET","http://www.xrss.cn", false);
oXmlHttp.send()
var oStream = new ActiveXObject("ADODB.Stream");
if(oStream == null)
alert("您的機(jī)器不支持ADODB.Stream.")
else
{
oStream.Type=1;
oStream.Mode=3;
oStream.Open() ;
oStream.Write(oXmlHttp.responseBody);
oStream.Position= 0;
oStream.Type= 2;
oStream.Charset="gb2312";
var result= oStream.ReadText();
oStream.Close();
oStream = null;
var aa = window.open("","")
document.write(result);
aa.document.write(result);
}
</script >
您可能感興趣的文章:
- asp.net獲取HTML表單File中的路徑的方法
- asp.net 中將表單提交到另一頁(yè) Code-Behind(代碼和html在不同的頁(yè)面)
- asp.net利用后臺(tái)實(shí)現(xiàn)直接生成html分頁(yè)的方法
- Asp.net動(dòng)態(tài)生成html頁(yè)面的方法分享
- 使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
- jquery獲取ASP.NET服務(wù)器端控件dropdownlist和radiobuttonlist生成客戶(hù)端HTML標(biāo)簽后的value和text值
- 使用ASP.NET 2.0 CSS 控件適配器生成CSS友好的HTML輸出
- asp.net生成HTML
- 利用ASP.NET技術(shù)動(dòng)態(tài)生成HTML頁(yè)面
- asp.net 防止用戶(hù)通過(guò)后退按鈕重復(fù)提交表單
- asp.net 模擬提交有文件上傳的表單(通過(guò)http模擬上傳文件)
- ASP.NET中實(shí)現(xiàn)把form表單元素轉(zhuǎn)為實(shí)體對(duì)象或集合
- asp.net動(dòng)態(tài)生成HTML表單的方法
相關(guān)文章
.NET中創(chuàng)建對(duì)象的幾種方式和對(duì)比
在 .net 中,創(chuàng)建一個(gè)對(duì)象最簡(jiǎn)單的方法是直接使用 new (), 在實(shí)際的項(xiàng)目中可能還會(huì)用到反射,本文就介紹了幾種創(chuàng)建方法,感興趣的可以了解一下2021-07-07ASP.NET Core 使用Cookie驗(yàn)證身份的示例代碼
這篇文章主要介紹了ASP.NET Core 使用Cookie驗(yàn)證身份的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02利用ASP.NET MVC+Bootstrap搭建個(gè)人博客之praise.js點(diǎn)贊特效插件(二)
這篇文章主要介紹了利用ASP.NET和MVC+Bootstrap搭建個(gè)人博客之praise.js點(diǎn)贊特效插件(二)的相關(guān)資料,需要的朋友可以參考下2016-06-06Asp.net?core中依賴(lài)注入的實(shí)現(xiàn)
這篇文章介紹了Asp.net?core中依賴(lài)注入的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07解析如何利用一個(gè)ASP.NET Core應(yīng)用來(lái)發(fā)布靜態(tài)文件
本文主要通過(guò)一些簡(jiǎn)單的實(shí)例來(lái)體驗(yàn)一下如何在一個(gè)ASP.NET Core應(yīng)用中發(fā)布靜態(tài)文件。針對(duì)不同格式的靜態(tài)文件請(qǐng)求的處理,ASP.NET Core為我們提供了三個(gè)中間件,它們將是本系列文章論述的重點(diǎn)。有需要的朋友可以看下2016-12-12彈出窗口,點(diǎn)擊確定在刪除數(shù)據(jù)的實(shí)現(xiàn)方法
彈出窗口,點(diǎn)擊確定在刪除數(shù)據(jù)的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-04-04C#數(shù)據(jù)綁定控件中的DataSource屬性淺談
使用該屬性指定用來(lái)填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對(duì)象, 如用于訪問(wèn)數(shù)據(jù)庫(kù)的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對(duì)象2013-02-02