ASP.NET实现伪静态代码示例
伪静态能利于搜索引擎收录站点,也能减少服务器压力今天共享一个伪静态实现方法,此方法实现在一般处理程序中。
代码仅供参考
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("", book.Title)
.Replace("", book.Author)
.Replace("", book.PublishDate.ToString())
.Replace("", book.ISBN)
.Replace("", book.UnitPrice.ToString())
.Replace("", 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");
楼主亲测成功 , 如有疑问请评论留言
谢谢分享!!! 谢谢分享!!!
页:
[1]