程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.net(c#) 生成html的幾種解決方案[思路]第1/2頁

ASP.net(c#) 生成html的幾種解決方案[思路]第1/2頁

編輯:ASP.NET基礎
方案1: 
復制代碼 代碼如下:
/// <summary >
/// 傳入URL返回網頁的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 "";
}

你可以用這個函數獲取網頁的客戶端的html代碼,然後保存到.html文件裡就可以了。
方案2:
生成單個的靜態頁面不是難點,難的是各個靜態頁面間的關聯和鏈接如何保持完整;特別是在頁面頻繁更新、修改、或刪除的情況下; 像阿裡巴巴的頁面也全部是html的,估計用的是地址映射的功能
關於地址映射可參考:http://www.easewe.com/Article/ShowArticle.aspx?article=131
可以看看這個頁面,分析一下他的“競價倒計時”功能
http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5
ASP.Net生成靜態HTML頁
在Asp中實現的生成靜態頁用到的FileSystemObject對象!
在.Net中涉及此類操作的是System.IO
以下是程序代碼 注:此代碼非原創!參考別人代碼
復制代碼 代碼如下:
//生成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";
// 替換內容
// 這時,模板文件已經讀入到名稱為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;

此函數放在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 >

提示添加成功後會出以當前時間為文件名的html文件!上面只是把傳遞過來的幾個參數直接寫入了HTML文件中,在實際應用中需要先添加數據庫,然後再寫入HTML文件
方案3:
給一個客戶端參考的例子(SJ)
它的作用在於以客戶端的方式獲取某個頁面的代碼,然後可以做為其他用途,本例是直接輸出
復制代碼 代碼如下:
<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("您的機器不支持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 >

當前1/2頁 12下一頁閱讀全文
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved