1、拆中做法、 、 判斷靜態檔案是否存在、不存在就建立一個(提前編輯好一個靜態頁模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<cebter>
<fieldset>
<legend>詳細資料</legend>
<table>
<tr><td>書名:</td><td>[$title]</td></tr>
<tr><td>作者:</td><td>[$author]</td></tr>
<tr><td>出版社:</td><td>[$publisher]</td></tr>
</table>
</fieldset>
</cebter>
</body>
</html>
、最後根據預留位置得到靜態頁面、 、 、 、)。
存在就直接轉向靜態檔案、 、
2、配置web.config檔案、
<httpHandlers>
<add verb="*" type="HtmlHandler,App_Code" path="*.html"/>
</httpHandlers>
3、實現 IHttphandler 類、 、
public void ProcessRequest(HttpContext context)
{
context.Request.ContentEncoding = Encoding.GetEncoding("utf-8"); // 編碼格式 、 、
context.Response.ContentEncoding = Encoding.GetEncoding("utf-8");
string url = context.Request.RawUrl; // 獲得請求的原始 url 、、
string temp = url.Remove(url.Length - 5);
StringBuilder sb = new StringBuilder();
foreach (char c in temp.Skip(temp.LastIndexOf('_')+1))
{
sb.Append(c.ToString());
} // 取出 id 、、
//context.Response.Write(sb);
string urlPath = "~/htmls/book" + sb;
lock(context.Application) // 應用程式一次只能訪問一個。 。
{
if (!File.Exists(context.Server.MapPath(urlPath)))
{
//如果不存在當前路徑對應的檔案則組建檔案
string strHtml = string.Empty;
using (StreamReader sr = new StreamReader(context.Server.MapPath("~/htmls/Template.htm")))
{
strHtml = sr.ReadToEnd();
}
strHtml = strHtml.Replace("[$title]", "C#進階編程");
strHtml = strHtml.Replace("[$author]", "張小三");
strHtml = strHtml.Replace("[$publisher]", "華爾街金融出版社");
using(StreamWriter sw=new StreamWriter(context.Server.MapPath(urlPath))){
sw.Write(strHtml);
}
}
}
context.Server.Execute(urlPath);
}