日期:2014-05-17  浏览次数:20394 次

Server.Execute生成静态页的问题。(有码)
目录是这样的:

Manage(文件夹)
  |-----AddInfo.aspx
Info(文件夹)
  |-----Show.aspx

AddInfo.aspx页面是添加信息页面,Show.aspx页面是根据url中id参数来显示信息。
目前的做法是,在添加完信息后,用Server.Execute方法访问Show.aspx页面,生成静态页面,但是报错,错误信息为“为 ../Info/Show.aspx 执行子请求时出错。”

生成静态页代码如下:
C# code

//添加信息操作,此处略。
//生成静态页
StringWriter writer = new StringWriter();
string url = "../Info/Show.aspx?id=12";   //此处url写死,做测试用
HttpContext.Current.Server.Execute(url, writer,true); //此处报错,“为 ../Info/Show.aspx 执行子请求时出错。”
File.WriteAllText(@"D:\static\1.html",HttpUtility.HtmlEncode(writer.ToString()));
writer.Close();
writer.Dispose();



网上没找到相关资料,谢谢了。

------解决方案--------------------
C# code
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;

此函数放在Conn.CS基类中了在添加新闻的代码中引用 注:工程名为Hover

    if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
    {
     Response.Write("添加成功");
    }
    else
    {
     Response.Write("生成HTML出错!");
    }