如何将.aspx页面转化成静态页
如题,
这里面是个怎么样的过程?
谁能给说下?
还有转的之后,它与数据库服务器、web服务器的交互有什么变化?
减少了没?
[解决办法]
转成了静态页(HTML)还能与数据库交互吗?应该不行吧
你的意思是动态生成静态页面吧,有两种方式,一是基于服务器,一是基于客户端,服务端组件网上有,搜一下在线编辑,会有很多,我这有一套基于客户端的系统。
生成后,直接保存成文件就行了
[解决办法]
ASP.NET生成静态页面实现方法
<!--Main.Aspx-->
<%@ page language= "C# " %>
<%@ import namespace=System.IO %>
<script runat= "server ">
protected override void OnInit (EventArgs e)
{
int id;
try
{
id = int.Parse (Request.QueryString[ "id "]);
}
catch
{
throw (new Exception ( "页面没有指定id "));
}
string filename=Server.MapPath( "statichtml_ "+id+ ".html ");
//尝试读取已有文件
Stream s = GetFileStream (filename);
if (s != null)//如果文件存在并且读取成功
{
using (s)
{
Stream2Stream (s, Response.OutputStream);
Response.End ();
}
}
//调用Main_Execute,并且获取其输出
StringWriter sw = new StringWriter ();
Server.Execute ( "Main_Execute.aspx ", sw);
string content = sw.ToString ();
//输出到客户端
Response.Write(content);
Response.Flush();
//写进文件
try
{
using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding))
{
streamwriter.Write (content);
}
}
}
finally
{
//Response.End ();
}
}
static public void Stream2Stream (Stream src, Stream dst)
{
byte[] buf = new byte[4096];
while (true)
{
int c = src.Read (buf, 0, buf.Length);
if(c==0)
return;
dst.Write (buf, 0, c);
}
}
public Stream GetFileStream(string filename)
{
try
{
DateTime dt = File.GetLastWriteTime (filename);
TimeSpan ts=dt - DateTime.Now;
if(ts.TotalHours> 1)
return null; //1小时后过期
return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch
{
return null;
}
}
</script>
<!--Main_Execute.aspx-->
<%@ page language= "C# " %>
<html>
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
ID:
<%=Request.QueryString[ "id "]%>
</body>
</html>
<!--Main.Aspx-->
<%@ page language= "C# " %>
<%@ import namespace=System.IO %>
<script runat= "server ">
protected override void OnInit (EventArgs e)
{
int id;
try
{
id = int.Parse (Request.QueryString[ "id "]);
}
catch
{
throw (new Exception ( "页面没有指定id "));
}
string filename=Server.MapPath( "statichtml_ "+id+ ".html ");
//尝试读取已有文件
Stream s = GetFileStream (filename);
if (s != null)//如果文件存在并且读取成功
{
using (s)
{
Stream2Stream (s, Response.OutputStream);
Response.End ();
}
}
//调用Main_Execute,并且获取其输出
StringWriter sw = new StringWriter ();
Server.Execute ( "Main_Execute.aspx ", sw);
string content = sw.ToString ();
//输出到客户端
Response.Write(content);
Response.Flush();
//写进文件
try
{
using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding))
{
streamwriter.Write (content);
}
}
}
finally
{
//Response.End ();
}
}
static public void Stream2Stream (Stream src, Stream dst)
{
byte[] buf = new byte[4096];
while (true)
{
int c = src.Read (buf, 0, buf.Length);
if(c==0)
return;
dst.Write (buf, 0, c);
}
}
public Stream GetFileStream(string filename)
{
try
{
DateTime dt = File.GetLastWriteTime (filename);
TimeSpan ts=dt - DateTime.Now;
if(ts.TotalHours> 1)
return null; //1小时后过期
return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch
{
return null;
}
}
</script>
<!--Main_Execute.aspx-->
<%@ page language= "C# " %>
<html>
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
ID:
<%=Request.QueryString[ "id "]%>
</body>
</html>
其中原理是这样的。
Main_Execute.aspx是生成HTML的页面。
现在用Main.aspx来对它进行缓存.
过程如下:
首先根据页面参数算出文件名。(这个例子只根据Request.QueryString[ "id "]来算)
尝试读取缓存的文件.如果成功,那么Response.End();
如果不成功:
使用Server.Execute来调用Main_Execute.aspx,并且获取它的结果内容。
得到内容后,立刻输出到客户端。
最后把内容写进文件里,提供给下一次做为缓存度取。
[解决办法]
当然能与数据库交互
方法很多,可以用iframe 也可以用 html控件提交到表单。
关于生成的话,我一般比较喜欢 实在的, 用.net的 IO类。
[解决办法]
高手的话,把js写到静态页里,ajax应用
普通水平的话,用iframe
想不开的话,就不要生成静态页了
[解决办法]
marp