zoukankan      html  css  js  c++  java
  • 用模版页的形式生成静态页

    这个功能在学校做过,却因为好久没用,又不太记得,所以又拿出来重写一遍加深一下映像,也希望能给一些人一些帮助。

    一般产品详细,新闻文章用的较多,我就以新闻中心为例:

    首先,创建一个简单模版页:newPage.htm

    <!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>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
            ul,li{ list-style:none; margin:0; padding:0;}
            #div{ 900px; margin:0 auto;}
            #top{ 100%; background-color:Gray; display:inline-block;}
            #left{ 75%; min-height:800px; display:inline-block; float:left;}
            #right{ 25%;min-height:800px; display:inline-block; background-color:Gray;}
            #main{ 95%; margin:0 auto;}
        </style>
    </head>
    <body>
        <div id="div">
            <div id="top">
                <h1>新闻中心</h1>
            </div>
            <div id="left">
                <div id="main">
                    <h1 style=" text-align:center;">[title]</h1>
                    <p>&nbsp;<label style=" 500px; display:inline-block;">作者:[author]</label><span>时间:[time]</span></p>
                    <div>[Content]</div>
                </div>
            </div>
            <div id="right">
    
            </div>
        </div>
        
    </body>
    </html>

    页面效果:

    然后,新建一个类(让它继承IHttpHandler,并实现该接口的方法):

    public void ProcessRequest(HttpContext context)
            {
                //例: url="http://localhost:5328/new_id.com"
                int str = context.Request.Path.LastIndexOf("_");
                int end = context.Request.Path.LastIndexOf(".");
                string id = context.Request.Path.Substring(str+1,end-str-1);//获取url中的id
                Contents con = new ContentsManager().GetContentsByID(Convert.ToInt32(id));//获取新闻内容
                string newsPath = context.Server.MapPath("~/FristPage/NewsContent/new_" + id + ".html");
                context.Application.Lock();
                if (!File.Exists(newsPath))
                {
                    string modlePath = context.Server.MapPath("~/FristPage/NewDetail.htm");//获得模版的路径
                    string template = null;
                    using (StreamReader strRead = new StreamReader(modlePath))
                    {
                        template = strRead.ReadToEnd();//读取模版页的所有内容
                    }
                    string html = template.Replace("[author]", con.AddAuthor)
                        .Replace("[title]", con.CTitle)
                        .Replace("[time]", con.AddTime.ToString("yyyy-MM-dd"))
                        .Replace("[Content]", con.CContents);
                    using (StreamWriter strWrite = new StreamWriter(newsPath))
                    {
                        strWrite.Write(html);//写入静态化页面内容
                    }
                    
                }
                context.Application.UnLock();
                context.Server.Execute("~/FristPage/NewsContent/new_" + id + ".html");
            }

    最后,需要在配置文件中配置一下<httpHandlers>标签:

    <system.web>
    
            <httpHandlers>
              <add verb="*" path="~/FristPage/NewsContent/*.html" type="Enterprise.axhx.CreateNewHandler"/>
            </httpHandlers>
        </system.web>
    "~/FristPage/NewsContent/new_1.html"是触发IHttpHandler去执行axhx,如果已经存在了读取,没有时,创建html页面

     
  • 相关阅读:
    mongodb导入导出
    python笔记1
    C# 文件下载断点续传
    热水维修记事
    memcached笔记
    模拟登陆
    Nginx学习笔记之加强篇
    Redis学习笔记之基础篇
    Nginx学习笔记之应用篇
    Nginx 学习笔记之安装篇
  • 原文地址:https://www.cnblogs.com/chen54/p/2820552.html
Copyright © 2011-2022 走看看