溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Asp.Net動態(tài)頁面的轉(zhuǎn)換方法

發(fā)布時間:2021-07-15 09:32:10 來源:億速云 閱讀:96 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“Asp.Net動態(tài)頁面的轉(zhuǎn)換方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Asp.Net動態(tài)頁面的轉(zhuǎn)換方法”吧!

關(guān)于在Asp.Net動態(tài)頁面轉(zhuǎn)靜態(tài)頁面的方法網(wǎng)上比較多。結(jié)合實際的需求,我在網(wǎng)上找了一些源代碼,并作修改。現(xiàn)在把修改后的代碼以及說明寫一下。

一個Asp.Net動態(tài)頁面轉(zhuǎn)換的類,該類通過靜態(tài)函數(shù)Changfile()來實現(xiàn),Asp.Net動態(tài)頁面到靜態(tài)頁面的轉(zhuǎn)換。

using System;  using System.Data;  using System.Configuration;  using System.Web;  using System.Web.Security;  using System.Web.UI;  using System.Web.UI.WebControls;  using System.Web.UI.WebControls.WebParts;  using System.Web.UI.HtmlControls;  using System.Text;  using System.IO;  /**////  /// Summary description for HtmlProxy  ///  public class HtmlProxy  ...{  public HtmlProxy()  ...{  }  public static bool ChangeFile(int id)  ...{  string filename = HttpContext.Current.Server.MapPath("Post_" + id + ".html");  //嘗試讀取已有文件 Stream st = GetFileStream(filename);  //如果文件存在并且讀取成功  if (st != null)  ...{  using (st)  ...{  StreamToStream(st, HttpContext.Current.Response.OutputStream);  return true;  //Response.End();  }  }  else  ...{  StringWriter sw = new StringWriter();  HttpContext.Current.Server.Execute("ForumDetail.aspx?PID=" + id, sw);  string content = sw.ToString();  //寫進(jìn)文件   try  ...{  using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))  ...{  using (StreamWriter stw = new StreamWriter(fs, HttpContext.Current.Response.ContentEncoding))  ...{  stw.Write(content);  }  }  return true;  }  catch ...{ return false; }  }  }  private static Stream GetFileStream(string filename)  ...{  try  ...{  DateTime dt = File.GetLastWriteTime(filename);  TimeSpan ts = dt - DateTime.Now;  if (ts.TotalHours >1)  ...{  //一小時后過期  return null;  }  return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);  }  catch ...{ return null; }  }  static public void StreamToStream(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);  }  }  }  在頁面文件中,F(xiàn)orURL.aspx的后臺代碼如下:  protected void Page_Load(object sender, EventArgs e)  ...{  try  ...{  int id = int.Parse(Request.QueryString["PID"]);  if(HtmlProxy.ChangeFile(id))  ...{  Response.Redirect("Post_" + id + ".html");  }  else  ...{  Response.Redirect("Post.aspx?PID=" + id );  }  }  catch ...{  }  }

到此,相信大家對“Asp.Net動態(tài)頁面的轉(zhuǎn)換方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI