独上高楼网站
  •    你所在位置:首页 VS.netASP.net编程经验〉大型网站新闻系统如何生成静态网页[摘]
  • 大型网站新闻系统如何生成静态网页[摘]
  • 作者:佚名  文章来源:网上下载  发布日期:2007-05-26  浏览次数:905
  • 打印这篇文章
  •  大型网站的新闻系统 一般都是以静态html文件的形式出现,这样做的好处就是对搜索引擎友好,能保证搜索引擎有效地收录网站内容.

    下面我提出两种解决办法,首先看看微软的解决办法.

    方法一:微软的解决办法:

    This step-by-step article describes how to retrieve the HTML results of a page as a stream and then download the stream to a file. When you use the FileStream object and set the Response.Filter property to the FileStream object, all HTTP output that Response.Write sends also downloads as stream to a file.

    Create a Web Form

    To create a Web Form:

    1.

     In Visual C# .NET, create a new ASP.NET Web Application project named ASPNETFilter.

     2.

     Right-click the designer pane of WebForm1.aspx.

     3.

     Click View HTML Source to edit the HTML code.

     4.

     Replace the existing code with the following code :

    〈%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ASPNETFilter.WebForm1" %>

    〈HTML>

            〈HEAD>

                  

            〈/HEAD>

            〈body>

                   〈form method="post" runat="server">

                           〈asp:TextBox Text="Textbox 1" Runat="server" />

                           〈asp:ListBox Runat="server" Size="3">

                                   〈asp:ListItem Value="0">Zero

                                   〈asp:ListItem Value="1" Selected="True">One

                                   〈asp:ListItem Value="2">Two

                           〈/asp:ListBox>〈br>

                           〈asp:CheckBox Runat="server" Checked="True" Text="Checkbox 1" />

                   〈/form>

            〈/body>

    〈/HTML>

     

    Create the ResponseFilter Class

    To create the ResponseFilter class:

    1.

     Add a new class named ResponseFilter.cs.

     2.

    Replace the existing code with the following code:

    using System;

    using System.IO;

     namespace ASPNETFilter

    {

            public class ResponseFilter : Stream

            {

                   private Stream m_sink;

                   private long m_position;

                   private FileStream fs;

     

                   public ResponseFilter(Stream sink)

                   {

                           m_sink = sink;

                           fs = new FileStream(@"C:\FilterOutput\response.htm", FileMode.OpenOrCreate, FileAccess.Write);

                   }

                    // The following members of Stream must be overriden.

                   public override bool CanRead

                   {get { return true; }}

     

                   public override bool CanSeek

                   {get { return false; }}

                    public override bool CanWrite

                   {get { return false; }}

                    public override long Length

                   {get { return 0; }}

                    public override long Position

                   {

                           get { return m_position; }

                           set { m_position = value; }

                   }

                    public override long Seek(long offset, System.IO.SeekOrigin direction)

                   {

                           return 0;

                   }

                    public override void SetLength(long length)

                   {

                           m_sink.SetLength(length);

                   }

                    public override void Close()

                   {

                           m_sink.Close();

                           fs.Close();

                   }

                    public override void Flush()

                   {

                           m_sink.Flush();

                   }

                    public override int Read(byte[] buffer, int offset, int count)

                   {

                           return m_sink.Read(buffer, offset, count);

                   }

                    // Override the Write method to filter Response to a file.

                   public override void Write(byte[] buffer, int offset, int count)

                   {             

                           //Write out the response to the browser.

                           m_sink.Write(buffer, 0, count);

            

                           //Write out the response to the file.

                           fs.Write(buffer, 0, count);   

                   }

            }

    }

    Note Before you run the Web application:

    1.

     Create a folder named C:\FilterOutput.

     

    2.

     Grant read and write access on the folder for the ASPNET user.

     

      

    Use the Response Filter Class

    1.

     In Solution Explorer, select WebForm1.aspx.

     2.

     Right-click and then select View Code.

     3.

     Add the following code to theOnInit event code:

    Response.Filter = new ResponseFilter(Response.Filter);

     

    Test the ResponseFilter

    1.

     Save the changes to the ASPNETFilter Web project.

     2.

     On the Build menu, select Build Solution.

     3.

     Start Microsoft Internet Explorer, and then open WebForm1.aspx by specifying the following URL, where IISServerName is the name of your Microsoft Internet Information Services (IIS) server:

    http://IISServerName/ASPNETFilter/WebForm1.aspx

     4.

     Find the Response.htm file in the C:\FilterOutput folder.

     

    方法二:其他解决办法:

    public static bool WriteFile(string strId, string strOldPath, string strNewPath)

    {

          string text1 = HttpContext.Current.Server.MapPath(strNewPath);

          Encoding encoding1 = Encoding.GetEncoding("gb2312");

          string text2 = strOldPath;

          StreamWriter writer1 = null;

          byte[] buffer1 = new WebClient().DownloadData(text2);

          string text3 = strId + ".htm";

          string text4 = Encoding.GetEncoding("gb2312").GetString(buffer1);

          text4 = text4.Replace("UserSearch", "");

         

    if (object.Equals(strId, "index.htm"))

          {

                text4 = text4.Replace("../", "");

          }

          if (object.Equals(strNewPath, "../musiclist/mmc_"))

          {

               

    text4 = text4 + "

  • 打印这篇文章
  • 与本文主题相关的文章
  • 返回首页