控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

html的静态化与httphander使用

作者:佚名 出处:互联网 2011年01月28日 阅读:

html的静态化与httphander使用 
 
一. Html的静态化:(将当前打开的aspx页面进行html静态化)

在aspx.cs页面中重写Render方法,解析成Html静态源码

     /// <summary>

    /// 重写Render方法,解析成Html静态源码

    /// </summary>

    protected override void Render(HtmlTextWriter writer)

    {

        TextWriter sw = new StringWriter();

        base.Render(new HtmlTextWriter(sw));

        string htmlName = test.html;

        CreateHtml(htmlName, sw.ToString());

     }

    /// <summary>

    /// 生成Html静态模板

    /// </summary>

    public void CreateHtml( string htmlName, string htmlBody)

    {

          StringReader sr = new StringReader(htmlBody);

        StringWriter sw = new StringWriter();

        string htmlLine = sr.ReadLine();

        try

        {

            while (htmlLine != null)

            {

                // 去掉Asp.Net独有标记

               if (!(htmlLine.IndexOf("<form") > 0 || htmlLine.IndexOf("__VIEWSTATE") > 0 || htmlLine.IndexOf("__EVENTVALIDATION") > 0 || htmlLine.IndexOf("</form>") > 0))

                {

                    sw.WriteLine(htmlLine);

                }

                htmlLine = sr.ReadLine();

            }

             string path = HttpContext.Current.Server.MapPath("~/Html/7/");    

            //判断文件目录是否存在

            if (!Directory.Exists(path))

            {        

                Directory.CreateDirectory(path);              

            }  

            //写入Html

                using (StreamWriter fs = new StreamWriter(path + htmlName, false, Encoding.GetEncoding("UTF-8")))

                {

                    try

                    {             

                        fs.Write(sw.ToString());       

                    }

                    catch (Exception ea)

                    {

                       // UserOperationLog("CreateHtml", "写入Html" + ea.Message);

                    }

                    finally

                    {    

                        if (fs != null)

                        {

                            fs.Flush();

                            fs.Close();

                        }

                    }

                }

            HttpContext.Current.Response.Write(sw.ToString());

        }

        catch (Exception ex)

        {

            UserOperationLog("CreateHtml出现异常", ex.Message);

            HttpContext.Current.Response.Write(ex.Message);

        }

        finally

        {

            HttpContext.Current.Response.End();

        }

}

 

二.httpHander使用:

1.在web.config 中设置:

<httpHandlers>

              <add verb="*" path="*.html" type="PortalHttpHandler,App_Code"/>

     </httpHandlers>

即,如果url是以html结尾的,则跳转到App_Code下的PortalHttpHandler类中进行处理;

1.   PortalHttpHandler.cs中,PortalHttpHandler类继承接口 IHttpHandler 如下:

public class PortalHttpHandler : IHttpHandler, IRequiresSessionState

{

    public PortalHttpHandler()

    {

        // TODO: 在此处添加构造函数逻辑

    }

    #region IHttpHandler 成员

    public bool IsReusable

    {

        get { return true; }

    }

    /// <summary>

    /// Url请求处理

    /// </summary>

    /// <param name="context">上下文</param>

    public void ProcessRequest(HttpContext context)

{

    //对url传递过来的内容进行处理;

 

    //处理完后进行跳转

ResponseHtmlCode(“test.html”,”test.aspx”,true)

   }

    #endregion

  

     /// <summary>

    /// 如果需要更新,直接跳转到相应的aspx页面,如果不需要更新跳转到对应的html页面

    /// </summary>

    /// <param name="context">上下文</param>

    public void ResponseHtmlCode(string path, string tempUrl, bool IsUpdated)

    {

        if (IsUpdated)

        {//如果需要更新,直接跳转到相应的aspx页面

            HttpContext.Current.Server.Transfer(tempUrl);

        }

        else

        {//如果不需要更新跳转到对应的html页面

            StreamReader sr = null;

            try

            {

                sr = new StreamReader(path);

                HttpContext.Current.Response.Write(sr.ReadToEnd());

            }

            catch (Exception e)

            {

                //写日志

            }

            finally

            {

                sr.Close();

                sr.Dispose();

                HttpContext.Current.Response.End();

            }

        }

    }

}

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统