zoukankan      html  css  js  c++  java
  • asp.net 网站纯静态化设计及其实现

    引言:为减轻服务器压力,较少伪静态对CPU的消耗,下面使用了纯静态的方式提供站点访问!

    一、流程图如下

    二、逐步分析

        A.捕获404,获取请求地址

        用户访问站点地址如下(例如:www.yahoo.com/news/1.html)

       这时如果站点中存在如下文件即可快速的完成访问,为什么呢?因为是真实的,纯html文件呗!

       不存在,那就是这套流程发挥效用的时候了!找不到文件,你肯定能得到404的错误

        这时在Global.asax文件中,写下如下代码

        

     1   protected void Application_Error(object sender, EventArgs e)
     2         {
     3 
     4             Exception error = Server.GetLastError();
     5 
     6             HttpException httpException = error as HttpException;
     7 
     8             if (httpException != null)
     9             {
    10                 int httpCode = httpException.GetHttpCode();
    11 
    12                 if (httpCode == 404)
    13                 {
    14 
    15                     Server.ClearError();
    16                     string ruleType, htmlcache, cacheName;
    17                     string errorUrl = Request.RawUrl;//请求页面地址      
    18 }
    19 } 20 }

         B.静态地址和动态地址的转化

         到目前为止你得到了页面的真是请求地址,(例如:www.yahoo.com/news/1.html),这时你需要做一个转换,将真是的请求地址转换为动态(aspx)地址,那么转换的关系的配置文件,我配置在一个xml中使用url rewriter格式编写

         如下:

    1     <RewriterRule>
    2       <LookFor>/news/(\d+).html</LookFor>
    3       <SendTo>News.aspx?pageIndex=$0</SendTo>
    4     </RewriterRule>

         转化方法略

         C.发送HTTP请求,向动态地址请求html内容,在Global.asax直接Response.Write(“返回的html”);

           

     1             WebRequest wrt = null;
     2             WebResponse wrse = null;
     3 
     4             try
     5             {
     6                 wrt = WebRequest.Create("真实地址");
     7                 wrse = wrt.GetResponse();
     8                 Stream strM = null;
     9                 StreamReader SR = null;
    10 
    11                 try
    12                 {
    13                     strM = wrse.GetResponseStream();
    14                     SR = new StreamReader(strM, code);
    15 
    16                     string strallstrm = SR.ReadToEnd();
    17                     return strallstrm;
    18                 }
    19                 catch (Exception ex)
    20                 { }
    21            }

          D.异步生成静态文件,供下次访问(异步请求的好处,用户不必等待,完成获取内容用户即可看到网页,生成静态页面由另一条线程完成)

     1  public delegate bool MakeHtmlDelegate(string htmlContent, string path);//htmlContent html内容,path 文件存放目录及其文件名称
     2                                 AsyncCallback callBack = new AsyncCallback(Procdss);
     3                                 MakeHtmlDelegate deleg = new MakeHtmlDelegate(生成方法);
     4                                 IAsyncResult async = deleg.BeginInvoke(htmlContent,path), callBack, deleg);
     5 
     6    void Procdss(IAsyncResult async)
     7         {
     8             MakeHtmlDelegate deleg = async.AsyncState as MakeHtmlDelegate;
     9             if (deleg != null)
    10             {
    11                 bool isSuc = deleg.EndInvoke(async);
    12             }
    13         }

           生成方法略,使用I0即可

    可以拍砖了。。。。。。。。。。。。

  • 相关阅读:
    详解股票买卖算法的最优解(一)
    Broker的主从架构是怎么实现的?
    和同事谈谈Flood Fill 算法
    聊一聊RocketMQ的注册中心NameServer
    你懂RocketMQ 的架构原理吗?
    常见的消息中间件有哪些?你们是怎么进行技术选型的?
    什么是消息中间件?主要作用是什么?
    @staticmethod
    Pandas 简介
    pytorch 不同版本对应的cuda
  • 原文地址:https://www.cnblogs.com/DevilMayCry/p/2699790.html
Copyright © 2011-2022 走看看