有个问题想请教前辈们!
我想执行本地网站的一个aspx文件,把它生成静态文件,
从网上收集到的是通过 //读取远程路径
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);这种要一个网页地址,有没有取相对路径来把aspx生成html的例子!谢谢!
自定义模板字符生成的这种方法不喜欢!!!
我想执行本地网站的一个aspx文件,把它生成静态文件,
从网上收集到的是通过 //读取远程路径
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);这种要一个网页地址,有没有取相对路径来把aspx生成html的例子!谢谢!
自定义模板字符生成的这种方法不喜欢!!!
解决方案 »
- webparts个性化定制自己的网店风格(好多人都解决不了)
- 在APS.NET中,如何动态修改web.config中的值呢?
- 根据ip判断打开一个特地的网站,在线等
- 在GridView中, 只对某一列进行编辑,有一个保存按钮(可以在gridview外面),单击时对修改的全部内容进行保存,请问要怎么做?
- 页面上的一个按钮(Button),点击后能否出现个询问是否继续的对话框?
- ajax 能否传dataset回服务器呢?
- .net2005 连接服务器数据库(sql 2000)无法访问问题
- 关于money类型格式转换的问题
- 下面的代码错在哪里?请教使用过owc的大侠了!
- 怎么在组件调用 web.conifig中ConfigurationSettings.AppSettings节的值???
- 倾家荡产求书~~
- asp.net数据绑定问题
此话怎么说?
别外,下面这段代码你明白什么意思吗?WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());//创建WebRequest对象
WebResponse myTemp = temp.GetResponse();//创建WebResponse,以得到指定网址的数据流
sr = new StreamReader(myTemp.GetResponseStream(), code);//以指定的编码方式将myTemp的数据流写入硬盘这与生html有什么关系?
把问题说清楚点吧。
protected override void OnInit(EventArgs e)
{
//根据一定规则生成静态页地址,当然要在Web目录下了,下面直接写的供参考
string htmlFilePath = @"d:\out.html";
if (Session["toActPage"] != null)
{
//清除标记
Session["toActPage"] = null;
//初次访问动态页,将请求交给IIS解析
base.OnInit(e);
}
else if (!System.IO.File.Exists(htmlFilePath))
{
Session["toActPage"] = true;
//生成静态页
System.IO.StreamWriter sw = new System.IO.StreamWriter(htmlFilePath, true, System.Text.Encoding.UTF8);
Server.Execute(Context.Request.RawUrl, sw, true);
sw.Close();
sw.Dispose();
}
//读取静态页
string content = ReadFile(htmlFilePath);
Response.Write(content);
//输出静态页内容后即中止请求。
//这样同样可以防止初次执行动态页行为被执行两次。
Response.End();
}
//读HTML内容
protected string ReadFile(string filePath)
{
string content = string.Empty;
System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
byte[] buf = new byte[2048];
int tempLen = 0;
fs.Position = 0;
while (fs.Position < fs.Length)
{
fs.Read(buf,0, buf.Length);
content += System.Text.Encoding.UTF8.GetString(buf);
}
fs.Close();
fs.Dispose();
return content;
}上面代码放在一个基类里供其它动态页继承 当然也可以写在自定义模块或句柄中(即实现IHttpModule或IHttpHandler的接口类) 注意文件编码格式 访问量大的站点还要考虑并发控制 不过普通站点已经能满足了
不说了
protected override void OnInit(EventArgs e)
{
if (Session["toActPage"] != null)
{
//清除标记
Session["toActPage"] = null;
//初次访问动态页,将请求交给IIS解析
base.OnInit(e);
}
else
{
string requestFile = string.Empty;
string[] part = Request.Path.Split(new char[] { '/' });
if (part != null && part.Length > 0)
{
requestFile = part[part.Length - 1];
}
//规则任意定的
string virPath=@"\html\" + requestFile.Replace(".", "") + ".html";
string phicicalPath = Request.MapPath(Request.ApplicationPath) + virPath;
if (!System.IO.File.Exists(phicicalPath))
{
Session["toActPage"] = true;
//生成静态页
System.IO.StreamWriter sw = new System.IO.StreamWriter(phicicalPath, true, System.Text.Encoding.UTF8);
Server.Execute(Context.Request.RawUrl, sw, true);
sw.Close();
sw.Dispose();
}
string htmlPath = "http://" + Request.ServerVariables["Http_Host"] + Request.ApplicationPath + "/html/" + requestFile.Replace(".", "") + ".html";
Response.Redirect(htmlPath);
}
}