马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

ASP.NET实现伪静态代码示例

[复制链接]
查看4300 | 回复2 | 2013-1-1 13:39:36 | 显示全部楼层 |阅读模式
伪静态能利于搜索引擎收录站点,也能减少服务器压力
今天共享一个伪静态实现方法,此方法实现在一般处理程序中。
代码仅供参考
[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");


楼主亲测成功 , 如有疑问请评论留言
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
chao2332601 | 2013-6-16 02:04:28 | 显示全部楼层
谢谢分享!!!
chao2332601 | 2013-6-16 04:59:09 | 显示全部楼层
谢谢分享!!!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则