伪静态能利于搜索引擎收录站点,也能减少服务器压力
今天共享一个伪静态实现方法,此方法实现在一般处理程序中。
代码仅供参考
[C#] 纯文本查看 复制代码 String path = context.Request.PhysicalPath;
int bookId=Convert.ToInt32(path.Substring(path.LastIndexOf("_") + 1, 4));
String filePath = context.Server.MapPath("~/BookDetails/Book_"+bookId+".html");
BookManager bookManager = new BookManager();
context.Application.Lock();
if (!File.Exists(filePath))
{
//模板路径
String templatePath = context.Server.MapPath("~/BookDetails/template.html");
String template = null;//模板内容
StreamReader rader = new StreamReader(templatePath);
template=rader.ReadToEnd();//读取模板内容
//根据Id查询出Book的信息
Book book=bookManager.GetBookById(bookId);
//写入内容
String htmlInfo =
template.Replace("[title]", book.Title)
.Replace("[author]", book.Author)
.Replace("[PublishDate]", book.PublishDate.ToString())
.Replace("[ISBN]", book.ISBN)
.Replace("[UnitPrice]", book.UnitPrice.ToString())
.Replace("[ContentDescription]", book.ContentDescription);
StreamWriter writer = new StreamWriter(filePath,false,Encoding.UTF8);
writer.Write(htmlInfo);//写入静态页面
writer.Close();
}
context.Application.UnLock();
context.Server.Execute("~/BookDetails/Book_" + bookId + ".html");
楼主亲测成功 , 如有疑问请评论留言
|