星空网 > 软件开发 > ASP.net

[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)

不使用特殊方法来处理请求生命周期事件

HttpApplication 类是全局应用类的基类,定义了可以直接使用的一般 C# 事件。那么使用标准 C# 事件还是特殊方法那就是个人偏好的问题了,如果喜欢,也可以将这两种方式混合起来使用。

[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)
 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Routing; 7  8 namespace SimpleApp 9 {10   public class MvcApplication : System.Web.HttpApplication11   {12     public MvcApplication()13     {14       BeginRequest += (src, args) => RecordEvent("BeginRequest");15       AuthenticateRequest += (src, args) => RecordEvent("AuthenticateRequest");16       PostAuthenticateRequest += (src, args) => RecordEvent("PostAuthenticateRequest");17     }18 19     protected void Application_Start()20     {21       AreaRegistration.RegisterAllAreas();22       RouteConfig.RegisterRoutes(RouteTable.Routes);23     }24 25     private void RecordEvent(string name)26     {27       List<string> eventList = Application["events"] as List<string>;28       if (eventList == null)29       {30         Application["events"] = eventList = new List<string>();31       }32       eventList.Add(name);33     }34   }35 }

View Code

我们已经在 MvcApplication 类中添加了一个构造器,并为其中三个请求生命周期事件建立事件处理器。而这三个事件我都使用了 lambda 表达式来调用 RecordEvent 方法,存储事件的名称供 controller 来读取,与之前的例子一样。

提示:没有标准的 C# 事件来代替 Application_Start 和 Application_End 方法。我们仅能够通过特殊方法来接收这两个通知。

使用单个方法处理多个事件

如果我们想要使用一个方法来处理多个生命周期事件而不依赖 lambda 表达式,那么 System.Web.HttpContext 类中定义的两个属性可以帮助我们。HttpContext 类中提供了当前请求和应用状态的详细信息,我们会在后面详细介绍。然而目前,我们只会介绍与处理生命周期事件的两个属性。

名称描述
CurrentNotification这个属性会使用 System.Web.RequestNotification 枚举中的值来指示当前应用事件。
IsPostNotification如果 CurrentNotification 属性返回的当前应用事件名称是 Post<Name> 的变体,那么这个属性就会返回 true。

这两个属性有一点奇怪,因为两者都需要计算出哪个事件正在被处理。CurrentNotification 属性返回一个 RequestNotification 枚举值,当中定义了 HttpApplication 事件的子集。IsPostNotification 属性值是根据当前触发的事件是否是一个像 AcquireRequestState 的事件还是与其配对的 PostAcquireRequestState 事件计算出来的。在 HttpApplication 类中,可以通过 Context 属性获取到 HttpContext 对象。

[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)
 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Routing; 7  8 namespace SimpleApp 9 {10   public class MvcApplication : System.Web.HttpApplication11   {12     public MvcApplication()13     {14       BeginRequest += RecordEvent;15       AuthenticateRequest += RecordEvent;16       PostAuthenticateRequest += RecordEvent;17     }18 19     protected void Application_Start()20     {21       AreaRegistration.RegisterAllAreas();22       RouteConfig.RegisterRoutes(RouteTable.Routes);23     }24 25     private void RecordEvent(object src, EventArgs args)26     {27       List<string> eventList = Application["events"] as List<string>;28       if (eventList == null)29       {30         Application["events"] = eventList = new List<string>();31       }32       string name = Context.CurrentNotification.ToString();33       if (Context.IsPostNotification)34       {35         name = "Post" + name;36       }37       eventList.Add(name);38     }39   }40 }

View Code

我改变了 RecordEvent 方法的签名,因此采用了标准的事件处理器签名:一个对象代表的是事件的源,EventArgs 对象描述的就是事件。我没有使用这两个参数提供的值,相反,我使用了 Context.CurrentNotification 属性和 Context.IsPostNotification 属性提供的相关信息。

我不明白微软为什么以这样的一个方式来实现事件,但是如果你不想使用特殊方法或者 lambda 表达式的话,那你就必须使用这种方式。注意到,在上面的代码中,我在 Context.CurrentNotification 属性上使用了 ToString 方法,这是必须的,因为 CurrentNotification 属性返回的是一个 System.Web.RequestNotification 枚举值。详见下表:

表 1 – RequestNotification 枚举值

描述
BeginRequest对应到 BeginRequest 事件
AuthenticateRequest对应到 AuthenticateRequest 和 PostAuthenticateRequest 事件
AuthorizeRequest对应到 AuthorizeRequest 事件
ResolveRequestCache对应到 ResolveRequestCache 和 PostResolveRequestCache 事件
MapRequestHandler对应到 MapRequestHandler和 PostMapRequestHandler 事件
AcquireRequestState对应到 AcquireRequestState 和 PostRequestState 事件
PreExecuteRequestHandler对应到 PreExecuteRequestHandler 事件
ExecuteRequestHandler对应到 ExecuteRequestHandler 事件
ReleaseRequestState对应到 ReleaseRequestState 和 PostReleaseRequestState 事件
UpdateRequestCache对应到 UpdateRequestCahce 事件
LogRequest对应到 LogRequest 事件
EndRequest对应到 EndRequest 事件
SendResponse指示响应正在被发送——不完全对应到 PreSendRequestHeaders 和 PreSendRequestContent 事件

 

[根据 Adam Freeman – Pro ASP.NET MVC 5 Platform 选译]




原标题:[译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

shopee产品批量上传:https://www.goluckyvip.com/tag/24960.html
shopee产品上架审核中:https://www.goluckyvip.com/tag/24961.html
shopee充值:https://www.goluckyvip.com/tag/24962.html
shopee促销活动:https://www.goluckyvip.com/tag/24963.html
shopee大促:https://www.goluckyvip.com/tag/24964.html
shopee大卖家:https://www.goluckyvip.com/tag/24965.html
丽人丽妆2023年扭亏为盈 玉容初、美壹堂等自有品牌增速超40% :https://www.kjdsnews.com/a/1836576.html
一天拿下10万美金预售!深圳大卖出海第一年成为亚马逊供应商:https://www.kjdsnews.com/a/1836577.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流