asp.net 生成靜態(tài)頁筆記
更新時(shí)間:2011年06月09日 23:48:40 作者:
asp.net 生成靜態(tài)頁筆記,需要在asp.net中生成靜態(tài)頁面的朋友可以參考下。
1.使用serever.Excute
StreamWriter sw = new StreamWriter(Server.MapPath("html/Login.html"), false);
Server.Execute("ShowColumn.aspx?id=1&page=2", sw);
sw.Close();
2.替換字符
url重寫
1.定義重寫規(guī)則
urls.xml 變成urls.config
<?xml version="1.0" encoding="utf-8" ?>
<Urls>
<rewrite name="ShowArticle" pattern="article-(\d+).html" path ="article-{0}.html" page="showarticle.aspx" query="id=$1"></rewrite>
<rewrite name="ShowList" pattern="list-(\d+).html" path ="list-{0}.html" page="showlist.aspx" query="id=$1"></rewrite>
</Urls>
2.創(chuàng)建一個(gè)簡單的實(shí)體urls類
3.urls類 獲取urls.config文件中的所有url
4.httpmodule類處理 請(qǐng)求的地址
5.在web.config httpmodule節(jié)點(diǎn)添加
asp.net生成靜態(tài)頁的兩種方法
Default.aspx頁面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Asp.net生成靜態(tài)頁的兩個(gè)例子</title>
</head>
<body>
<form id="form1" runat="server">
<div>
標(biāo)題:<asp:TextBox ID="txtTitle" runat="server" Width="352px"></asp:TextBox><br />
內(nèi)容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine"
Width="350px"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="根據(jù)模板生成" /><br />
<br />
<br />
Url地址:<asp:TextBox ID="txtUrl" runat="server" ToolTip="請(qǐng)確認(rèn)Url地址的存在" Width="359px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="根據(jù)Url地址生成" OnClick="Button2_Click" /></div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
namespace WebApplication6
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//源碼是替換掉模板中的特征字符
string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//讀取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
//根據(jù)時(shí)間自動(dòng)重命名,擴(kuò)展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$title$", txtTitle.Text);//替換Title
str = str.Replace("$content$", txtContent.Text);//替換content
//生成靜態(tài)文件
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經(jīng)生成,保存在htm文件夾下!");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//讀取遠(yuǎn)程路徑
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
//讀取
try
{
sr = new StreamReader(myTemp.GetResponseStream(), code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
//寫入
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經(jīng)生成,保存在htm文件夾下!");
}
}
}
}
復(fù)制代碼 代碼如下:
StreamWriter sw = new StreamWriter(Server.MapPath("html/Login.html"), false);
Server.Execute("ShowColumn.aspx?id=1&page=2", sw);
sw.Close();
2.替換字符
url重寫
1.定義重寫規(guī)則
urls.xml 變成urls.config
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<Urls>
<rewrite name="ShowArticle" pattern="article-(\d+).html" path ="article-{0}.html" page="showarticle.aspx" query="id=$1"></rewrite>
<rewrite name="ShowList" pattern="list-(\d+).html" path ="list-{0}.html" page="showlist.aspx" query="id=$1"></rewrite>
</Urls>
2.創(chuàng)建一個(gè)簡單的實(shí)體urls類
3.urls類 獲取urls.config文件中的所有url
4.httpmodule類處理 請(qǐng)求的地址
5.在web.config httpmodule節(jié)點(diǎn)添加
asp.net生成靜態(tài)頁的兩種方法
Default.aspx頁面:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Asp.net生成靜態(tài)頁的兩個(gè)例子</title>
</head>
<body>
<form id="form1" runat="server">
<div>
標(biāo)題:<asp:TextBox ID="txtTitle" runat="server" Width="352px"></asp:TextBox><br />
內(nèi)容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine"
Width="350px"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="根據(jù)模板生成" /><br />
<br />
<br />
Url地址:<asp:TextBox ID="txtUrl" runat="server" ToolTip="請(qǐng)確認(rèn)Url地址的存在" Width="359px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="根據(jù)Url地址生成" OnClick="Button2_Click" /></div>
</form>
</body>
</html>
Default.aspx.cs
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
namespace WebApplication6
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//源碼是替換掉模板中的特征字符
string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//讀取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
//根據(jù)時(shí)間自動(dòng)重命名,擴(kuò)展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$title$", txtTitle.Text);//替換Title
str = str.Replace("$content$", txtContent.Text);//替換content
//生成靜態(tài)文件
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經(jīng)生成,保存在htm文件夾下!");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//讀取遠(yuǎn)程路徑
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
//讀取
try
{
sr = new StreamReader(myTemp.GetResponseStream(), code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
//寫入
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經(jīng)生成,保存在htm文件夾下!");
}
}
}
}
您可能感興趣的文章:
- ASP.NET AJAX 4.0的模版編程(Template Programming)介紹
- asp.net實(shí)現(xiàn)生成靜態(tài)頁并添加鏈接的方法
- Asp.Net生成靜態(tài)頁面的實(shí)現(xiàn)方法
- ASP.NET動(dòng)態(tài)生成靜態(tài)頁面的實(shí)例代碼
- ASP.NET MVC3關(guān)于生成純靜態(tài)后如何不再走路由直接訪問靜態(tài)頁面
- 使用ASP.NET模板生成HTML靜態(tài)頁面的五種方案
- Asp.Net 生成靜態(tài)頁并實(shí)現(xiàn)分頁效果
- Asp.NET 生成靜態(tài)頁面并分頁的代碼
- ASP.NET 生成靜態(tài)頁面 實(shí)現(xiàn)思路
- asp.net 生成靜態(tài)頁時(shí)的進(jìn)度條顯示
- asp.net生成靜態(tài)頁并分頁+ubb
- asp.net基于替換模版頁的形式生成靜態(tài)頁的方法
相關(guān)文章
VS+opencv實(shí)現(xiàn)鼠標(biāo)移動(dòng)圖片
這篇文章主要為大家詳細(xì)介紹了VS+opencv實(shí)現(xiàn)鼠標(biāo)移動(dòng)圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01詳解ASP.NET MVC 解析模板生成靜態(tài)頁(RazorEngine)
我們?cè)诤芏囗?xiàng)目開發(fā)中會(huì)常常用到頁面靜態(tài)化,本篇文章主要介紹了詳解ASP.NET MVC 解析模板生成靜態(tài)頁(RazorEngine) ,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03ASP.NET從字符串中查找字符出現(xiàn)次數(shù)的具體實(shí)現(xiàn)方法
今天在一場“特殊的討論”中引入了一個(gè)問題,如何在C#求出字符串中某字符的出現(xiàn)次數(shù),比如求“ADSFGEHERGASDF”中“A”出現(xiàn)的次數(shù)2013-11-11asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類型)
這篇文章主要介紹了asp.net文件上傳解決方案,包括:圖片上傳、單文件上傳、多文件上傳、檢查文件類型等案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09