现在的位置: 首页 > 综合 > 正文

两行代码取得当前网站的(无交互状态下的)网页html源代码就够了,不用对asp.net网页设计技术进行一点点倒退

2013年04月25日 ⁄ 综合 ⁄ 共 1106字 ⁄ 字号 评论关闭
我写一个demo,在网站下创建一个aspx文件,把下面的代码拷贝进去,然后直接从浏览器上浏览这个页面:
HTML code
<%@ Page Language="C#" %>

<%@ Import Namespace="System.IO" %>

<script runat="server">
protected
void Button1_Click(object sender, EventArgs e)
{
StringWriter wr
= new StringWriter();
Server.Execute(
this.TextBox1.Text, wr); //你可以使用第三个参数传递页面的更多初始数据
this.Label1.Text = Server.HtmlEncode(wr.ToString());
File.WriteAllText(Server.MapPath(
this.TextBox2.Text), wr.ToString());
}
</script>

<!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 runat="server">
<title>演示取得网站中其它网页的源代码只需要两行代码就够了</title>
</head>
<body>
<form id="form1" runat="server" defaultbutton="Button1">
<div>
请输入本网站的页面名称:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
请输入目标文件名称
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><hr />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>

因为我一直不太喜欢保存html,我偏爱输出缓存,所以没有特意说过这个事。最近看到的从asp、jsp时代抄来的字符串替换的代码太多了,我觉得应该写一个demo说明asp.net2.0中如何最基本地生成html。

抱歉!评论已关闭.