国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 12:38:56
字體:
來源:轉載
供稿:網友
方案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();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和平区| 大英县| 东方市| 永寿县| 巴南区| 阿荣旗| 贺兰县| 荥经县| 兴国县| 葵青区| 锦州市| 北京市| 南岸区| 东光县| 宜宾县| 兰考县| 长汀县| 墨竹工卡县| 凤山县| 清苑县| 宁阳县| 芜湖市| 衡阳县| 扶沟县| 天全县| 盐边县| 贵阳市| 岑巩县| 宜君县| 四平市| 刚察县| 琼海市| 瓦房店市| 清原| 云浮市| 盐边县| 乌苏市| 巴南区| 延津县| 包头市| 靖边县|