欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 生成静态页笔记

程序员文章站 2024-03-07 22:24:58
1.使用serever.excute 复制代码 代码如下: streamwriter sw = new streamwriter(server.mappath("html/...
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.定义重写规则
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.创建一个简单的实体urls类
3.urls类 获取urls.config文件中的所有url
4.httpmodule类处理 请求的地址
5.在web.config httpmodule节点添加

asp.net生成静态页的两种方法

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生成静态页的两个例子</title>
</head>
<body>
<form id="form1" runat="server">
<div>
标题:<asp:textbox id="txttitle" runat="server" width="352px"></asp:textbox><br />
内容:<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="根据模板生成" /><br />
<br />
<br />
url地址:<asp:textbox id="txturl" runat="server" tooltip="请确认url地址的存在" width="359px"></asp:textbox>
<br />
<br />
<asp:button id="button2" runat="server" text="根据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();
}
//根据时间自动重命名,扩展名也可以自行修改
string filename = datetime.now.tostring("yyyymmddhhmmss") + ".htm";
str = str.replace("$title$", txttitle.text);//替换title
str = str.replace("$content$", txtcontent.text);//替换content
//生成静态文件
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>已经生成,保存在htm文件夹下!");
}
}
protected void button2_click(object sender, eventargs e)
{
encoding code = encoding.getencoding("utf-8");
streamreader sr = null;
streamwriter sw = null;
string str = null;
//读取远程路径
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>已经生成,保存在htm文件夹下!");
}
}
}
}