有个问题想请教前辈们!
我想执行本地网站的一个aspx文件,把它生成静态文件,
从网上收集到的是通过      //读取远程路径
      WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
      WebResponse myTemp = temp.GetResponse();
      sr = new StreamReader(myTemp.GetResponseStream(), code);这种要一个网页地址,有没有取相对路径来把aspx生成html的例子!谢谢!
自定义模板字符生成的这种方法不喜欢!!!

解决方案 »

  1.   

    也想过用补全:"http://" + Request.Url.Host + "/test.html",在网站系统调用计划任务等,不是通过执行aspx等页面的有问题吧???
      

  2.   

    问题描述不清,aspx生成html,
    此话怎么说?
    别外,下面这段代码你明白什么意思吗?WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());//创建WebRequest对象
    WebResponse myTemp = temp.GetResponse();//创建WebResponse,以得到指定网址的数据流
    sr = new StreamReader(myTemp.GetResponseStream(), code);//以指定的编码方式将myTemp的数据流写入硬盘这与生html有什么关系?
    把问题说清楚点吧。
      

  3.   

    WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());//创建WebRequest对象是用来获取aspx文件的内容的啊!!!
      

  4.   

    http://topic.csdn.net/u/20080624/10/26a45062-572c-47e9-bb38-d9aa05b6c2ed.html
      

  5.   

    楼主表述不太清楚 刚写了一个生成静态的方案 应该能满足楼主要求 代码如下(已测试):    //重写初始化方法
        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的接口类) 注意文件编码格式  访问量大的站点还要考虑并发控制 不过普通站点已经能满足了
      

  6.   

    哦 刚想了想 上面读取的部分多余了 可以直接访问 一个Response.Redirect就可以了 当然了静态文件路径要映射成Http格式的
      

  7.   

    还有 如上面的读取后输出的方式 地址栏里显示的仍是动态页(如.aspx文件) 但实际是读取的是静态文本内容 不要混了哦
    不说了
      

  8.   

    如果是本地的,直接用System.IO中的File类来读取不就行了
      

  9.   

    感动啊分全给我了 帮到底 上面可能会出现进程占用的问题 下面是改进版的:    //重写初始化方法
        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);
            }        
        }