ASP.net(c#) 生成html的幾種解決方案[思路]第1/2頁
更新時(shí)間:2009年05月17日 00:51:56 作者:
下面的文章轉(zhuǎn)載自網(wǎng)絡(luò),其代碼很多都有問題,這里只提供給大家一個(gè)思路.
方案1:
/// <summary >
/// 傳入U(xiǎn)RL返回網(wǎng)頁的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)頁的客戶端的html代碼,然后保存到.html文件里就可以了。
方案2:
生成單個(gè)的靜態(tài)頁面不是難點(diǎn),難的是各個(gè)靜態(tài)頁面間的關(guān)聯(lián)和鏈接如何保持完整;特別是在頁面頻繁更新、修改、或刪除的情況下; 像阿里巴巴的頁面也全部是html的,估計(jì)用的是地址映射的功能
關(guān)于地址映射可參考:http://www.easewe.com/Article/ShowArticle.aspx?article=131
可以看看這個(gè)頁面,分析一下他的“競價(jià)倒計(jì)時(shí)”功能
http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5
ASP.Net生成靜態(tài)HTML頁
在Asp中實(shí)現(xiàn)的生成靜態(tài)頁用到的FileSystemObject對象!
在.Net中涉及此類操作的是System.IO
以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼
//生成HTML頁
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)讀入到名稱為str的變量中了
str =str.Replace("ShowArticle",strText); //模板頁中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 寫文件
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基類中了
在添加新聞的代碼中引用 注:工程名為Hover
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出錯!");
}
模板頁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 >
提示添加成功后會出以當(dāng)前時(shí)間為文件名的html文件!上面只是把傳遞過來的幾個(gè)參數(shù)直接寫入了HTML文件中,在實(shí)際應(yīng)用中需要先添加數(shù)據(jù)庫,然后再寫入HTML文件
方案3:
給一個(gè)客戶端參考的例子(SJ)
它的作用在于以客戶端的方式獲取某個(gè)頁面的代碼,然后可以做為其他用途,本例是直接輸出
<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)頁的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)頁的客戶端的html代碼,然后保存到.html文件里就可以了。
方案2:
生成單個(gè)的靜態(tài)頁面不是難點(diǎn),難的是各個(gè)靜態(tài)頁面間的關(guān)聯(lián)和鏈接如何保持完整;特別是在頁面頻繁更新、修改、或刪除的情況下; 像阿里巴巴的頁面也全部是html的,估計(jì)用的是地址映射的功能
關(guān)于地址映射可參考:http://www.easewe.com/Article/ShowArticle.aspx?article=131
可以看看這個(gè)頁面,分析一下他的“競價(jià)倒計(jì)時(shí)”功能
http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5
ASP.Net生成靜態(tài)HTML頁
在Asp中實(shí)現(xiàn)的生成靜態(tài)頁用到的FileSystemObject對象!
在.Net中涉及此類操作的是System.IO
以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼
復(fù)制代碼 代碼如下:
//生成HTML頁
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)讀入到名稱為str的變量中了
str =str.Replace("ShowArticle",strText); //模板頁中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 寫文件
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基類中了
在添加新聞的代碼中引用 注:工程名為Hover
復(fù)制代碼 代碼如下:
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出錯!");
}
模板頁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 >
提示添加成功后會出以當(dāng)前時(shí)間為文件名的html文件!上面只是把傳遞過來的幾個(gè)參數(shù)直接寫入了HTML文件中,在實(shí)際應(yīng)用中需要先添加數(shù)據(jù)庫,然后再寫入HTML文件
方案3:
給一個(gè)客戶端參考的例子(SJ)
它的作用在于以客戶端的方式獲取某個(gè)頁面的代碼,然后可以做為其他用途,本例是直接輸出
復(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 中將表單提交到另一頁 Code-Behind(代碼和html在不同的頁面)
- asp.net利用后臺實(shí)現(xiàn)直接生成html分頁的方法
- Asp.net動態(tài)生成html頁面的方法分享
- 使用ASP.NET模板生成HTML靜態(tài)頁面的五種方案
- jquery獲取ASP.NET服務(wù)器端控件dropdownlist和radiobuttonlist生成客戶端HTML標(biāo)簽后的value和text值
- 使用ASP.NET 2.0 CSS 控件適配器生成CSS友好的HTML輸出
- asp.net生成HTML
- 利用ASP.NET技術(shù)動態(tài)生成HTML頁面
- asp.net 防止用戶通過后退按鈕重復(fù)提交表單
- asp.net 模擬提交有文件上傳的表單(通過http模擬上傳文件)
- ASP.NET中實(shí)現(xiàn)把form表單元素轉(zhuǎn)為實(shí)體對象或集合
- asp.net動態(tài)生成HTML表單的方法
相關(guān)文章
ASP.NET Core 使用Cookie驗(yàn)證身份的示例代碼
這篇文章主要介紹了ASP.NET Core 使用Cookie驗(yàn)證身份的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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中依賴注入的實(shí)現(xiàn)
這篇文章介紹了Asp.net?core中依賴注入的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07解析如何利用一個(gè)ASP.NET Core應(yīng)用來發(fā)布靜態(tài)文件
本文主要通過一些簡單的實(shí)例來體驗(yàn)一下如何在一個(gè)ASP.NET Core應(yīng)用中發(fā)布靜態(tài)文件。針對不同格式的靜態(tài)文件請求的處理,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屬性淺談
使用該屬性指定用來填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對象, 如用于訪問數(shù)據(jù)庫的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對象2013-02-02