zoukankan      html  css  js  c++  java
  • asp.net执行顺速

    理解ASP.NET的运行机制(例:通过HttpModule来计算页面执行时间)

    一:简要介绍一下asp.net的执行步骤

    1.IIS接收到客户请求

    2. IIS把请求交给aspnet_isapi.dll处理

    3.(如果是第一次运行程序)装载bin目录中的dll

    4.(如果是第一次运行程序)读取各级webconfig中的配置

    5.(如果是第一次运行程序)编译装载global.asax,初始化HttpApplication实例

    6.创建响应请求的HttpContext

    7.创建承载响应结果的HttpTextWriter

    8.找到合适的HttpHandler(asp.net页面),处理http请求

    9.处理session,异常

    10.把处理结果反馈个客户

    当HttpApplication实例创建后,
    会调用InitModules()方法,
    这个方法会根据webconfig文件中的配置调用相应的HttpModule
    这就是可定制的HttpModule

    二:通过定制HttpModule来计算页面执行时间


    当HttpApplication创建HttpModule时
    将会执行HttpModule的Init方法
    在这个方法中,可以订阅多个事件
    如下:
    BeginRequest
    AuthenticateRequest 当安全模块已建立用户标识时发生。
    PostAuthenticateRequest
    AuthorizeRequest 当安全模块已验证用户授权时发生。
    PostAuthorizeRequest
    ResolveRequestCache
    PostResolveRequestCache
    PostMapRequestHandler
    AcquireRequestState
    PostAcquireRequestState
    PreRequestHandlerExecute
    PostRequestHandlerExecute
    ReleaseRequestState
    PostReleaseRequestState
    EndRequest
    这些事件也是HttpApplication处理管线中的各种事件
    常用的就是BeginRequest和EndRequest

    下面我们做一个例子来实现计算页面的执行时间
    先看webconfig的代码

    <?xmlversionxmlversionxmlversionxmlversion="1.0"?>   
    <configuration>   
       <system.web>        
           <httpModules>   
               <add name="MyModule"type="xland.MyModule"/>   
           </httpModules>   
       </system.web>   
    </configuration>
     
    xland是我创建的一个类库,命名空间为xland
    MyModule为该类库下的一个类实现了IHttpModules接口
    下面看这个类的代码:


     
    using System;  
    usingSystem.Collections.Generic;  
    usingSystem.Web;//引用web命名空间   
    using System.Text;  
    namespace xland  
    {  
        public classMyModule:IHttpModule//继承IHttpModules   
        {  
              
           public void Init(HttpApplicationapplication)//实现IHttpModules中的Init事件   
            {  
               //订阅两个事件   
                application.BeginRequest +=newEventHandler(application_BeginRequest);  
                application.EndRequest+=newEventHandler(application_EndRequest);  
            }  
           private DateTimestarttime;  
           private void application_BeginRequest(object sender, EventArgse)  
            {  
               //objectsender是BeginRequest传递过来的对象   
               //里面存储的就是HttpApplication实例   
               //HttpApplication实例里包含HttpContext属性   
                starttime = DateTime.Now;  
                HttpApplication application =(HttpApplication)sender;  
                HttpContext context =application.Context;  
                context.Response.Write("开始计时,现在的时间为:" +starttime.ToString("yyyy年MM月dd日HH:mm:ss.fff") +"<br>");  
            }  
           private void application_EndRequest(object sender, EventArgse)  
            {  
                DateTime endtime =DateTime.Now;  
                HttpApplication application =(HttpApplication)sender;  
                HttpContext context =application.Context;  
                context.Response.Write("计时结束,现在的时间为:" +endtime.ToString("yyyy年MM月dd日HH:mm:ss.fff") +"<br>");  
                context.Response.Write("页面执行时间:" + (endtime -starttime).ToString());  
            }  
           //必须实现dispose接口   
           public void Dispose() { }  
        }  
    }
     
    好其他的都不用管,我们在default.aspx中做一些耗时的操作


    注意在web应用程序类库中一定要添加xland的引用

    using System;  
    using System.Collections;  
    usingSystem.Configuration;  
    using System.Data;  
    using System.Web;  
    usingSystem.Web.Security;  
    using System.Web.UI;  
    usingSystem.Web.UI.HtmlControls;  
    usingSystem.Web.UI.WebControls;  
    usingSystem.Web.UI.WebControls.WebParts;  
    namespace _1  
    {  
        publicpartial class _Default :System.Web.UI.Page  
        {  
           protected void Page_Load(object sender, EventArgse)  
            {  
               for (int i = 1; i < 10000;i++)  
                {                  
                    Response.Write(i);  
                   if (i % 100 == 0) {Response.Write("<br>");}  
                }  
            }  
        }  

    BeginRequest和EndRequest里可以做很多事情
    比如处理cookie的domain等
    等具体需要的时候要能想到这里来

  • 相关阅读:
    干掉:“请停用以开发者模式运行的扩展程序”
    电脑分区
    tomcat进行远程debug
    工作流审核到最后一步迟迟不能完成
    DataGrid首次进入页面时,不加载任何数据[转]
    用Excel进行个人敏捷项目看板管理
    cnblogs的编辑器BUG反馈:发布或编辑文章完成后,页面是卡死状态的
    程序员如何优雅地挣零花钱?
    SQL Server导入数据报错"无法在只读列“Id”中插入数据",几百个表怎么批量启用'启用标识插入'选项
    SQL Server 各版本发布时间、开发代号及下载地址
  • 原文地址:https://www.cnblogs.com/ws-zpp/p/7461770.html
Copyright © 2011-2022 走看看