将aspx网页以HTML形式存储的几个方法
来源:互联网 发布:手机淘宝怎么申请直播 编辑:程序博客网 时间:2024/05/21 11:18
aspx网页以HTML形式存储的几个方法
第一种是模版替换:
环境:Microsoft .NET Framework SDK v1.1
OS:Windows Server 2003 中文版
ASP.Net生成静态HTML页
在Asp中实现的生成静态页用到的FileSystemObject对象!
在.Net中涉及此类操作的是System.IO
以下是程序代码 注:此代码非原创!参考别人代码
此函数放在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文件.
第二种:
WebRequest访问aspx页面,然后获取Response流,得到的就是html
环境:Microsoft .NET Framework SDK v1.1
OS:Windows Server 2003 中文版
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();
}
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;
// 替换内容
// 这时,模板文件已经读入到名称为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文件.
第二种:
WebRequest访问aspx页面,然后获取Response流,得到的就是html
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text=this.GetUrlValue("http://YourUrl");
}
//使用HttpWebRequest获得URL的返回值
public string GetUrlValue(string url)
{
System.Net.WebRequest HttpWebRequest=System.Net.WebRequest.Create(url);
System.Net.WebResponse HttpWebResponse =HttpWebRequest.GetResponse();
System.IO.StreamReader sr=new System.IO.StreamReader(HttpWebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
return sr.ReadToEnd();
}
{
textBox1.Text=this.GetUrlValue("http://YourUrl");
}
//使用HttpWebRequest获得URL的返回值
public string GetUrlValue(string url)
{
System.Net.WebRequest HttpWebRequest=System.Net.WebRequest.Create(url);
System.Net.WebResponse HttpWebResponse =HttpWebRequest.GetResponse();
System.IO.StreamReader sr=new System.IO.StreamReader(HttpWebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
return sr.ReadToEnd();
}
http://www.cnblogs.com/guodashao/archive/2006/08/01/465045.html
- 将aspx网页以HTML形式存储的几个方法
- aspx网页以HTML形式存储的几个方法
- aspx网页以HTML形式存储的几个方法
- aspx网页以HTML形式存储的几个方法
- aspx网页以HTML形式存储的几个方法
- aspx生成HTML几个方法
- 将IP以整型的形式存储到数据库中
- 网页 选项卡的实现 拷贝后 以html形式保存即可
- nodejs怎么把html以源码的形式在网页中显示了,而不是解析它。
- 用Dreamweaver将数据库中的数据以柱状图的形式动态展示于网页中
- 将从数据库得到的信息分别以HTML,XML,Json的形式输出
- 将 html代码以文本内容的形式显示在 label 等控件上
- Excel表中如何将常规数字批量转换为以文本形式存储的数字?
- 将文件存储到数据库(以二进制流的形式)
- 将网页的解析数据存储为html文件
- 打开期刊-以html的形式打开
- 将页面以excel的形式导出
- 将图像以二进制形式存储及调用
- e45
- 路由协议之间 路由的重新分配
- 链接错误 "unresolved external symbol _main"
- 常用javascript函数
- 一些有用的SAP技术TCODE
- 将aspx网页以HTML形式存储的几个方法
- ASP.NET多线程范例1
- C#里的委托和事件实现Observer
- 800
- [.net]正则表达式整理
- “引起全美大学生举国辩论的逻辑题”的模拟实验(wizimkit外部工具)
- 文字或者图片向上滚动代码
- 在csdn blog正式成立!好不容易弄成现在这个样子
- 面向.NET开发人员的Ajax 技术平台策略