·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.NET MVC学前篇之请求流程

ASP.NET MVC学前篇之请求流程

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

asp.net MVC学前篇之请求流程

ASP.NET MVC学前篇之请求流程

请求流程描述

对于请求的流程,文章的重点是讲Httpapplication和HttpModule之间的关系,以及一个简单的示例实现。(HttpModule又是MVC框架的入口点)

图1

在请求到达Web服务器过后进入ASP.NET的时候是通过ASP.NET来构造出一个HttpWorkerRequest对象,HttpWorkerRequest是抽象类类型,表示着一些请求处理的信息,然后由ASP.NET中的HttPRuntime类型来调用静态函数ProcessRequest(),参数类型为HttpWorkerRequest,因为HttpWorkerRequest是抽象的,在使用的时候应该是系统内部会有个实现类。 在ProcessRequest()方法的内部产生HttpApplication对象,这之间的过程,已经把HttpWorkerPequest对象处理后转变到HttpRequest对象,HttpRequest对象是公开的可代码访问的(图中没有表示出来)。 这个时候还没有执行HttpHandler程序,而是先执行HttpModule中的内容,它们订阅了HttpApplication中的事件用于在请求的各种状态之间做一下自定义的修改(这些在下面的示例中会说到。 然后执行HttpHandler,在处理程序执行完毕后,不是到HttpResponse,而是又到了HttpModule中执行请求完成后的一些自定义操作,这是在HttpApplication中约定好的,在这些都完成的情况下才会做Response操作。

我们将在下面的示例中模拟的演示一下在HttpApplication类型中的事件使用模型。

示例部分

代码1-1

 1     public delegate void PassNotice(NoticeContext noticeContext); 2  3     public class Order 4     { 5         private NoticeContext _noticeContext; 6  7         public NoticeContext NoticeContext 8         { 9             get { return _noticeContext; }10             set { _noticeContext = value; }11         }12 13         private PassNotice _befPassNotice;14         public event PassNotice BefPassNotice15         {16             add17             {18                 _befPassNotice += value;19             }20             remove21             {22                 _befPassNotice -= value;23             }24         }25 26         private PassNotice _latPassNotice;27 28         public event PassNotice LatPassNotice29         {30             add31             {32                 _latPassNotice += value;33             }34             remove35             {36                 _latPassNotice -= value;37             }38         }39 40         private void SendGoods()41         {42             Console.WriteLine("发货…… 请等待接收");43         }44 45         public void Start()46         {47             if (_befPassNotice != null)48             {49                 _befPassNotice(NoticeContext);50             }51 52             if (NoticeContext.IsSend)53             {54                 Console.WriteLine("服务端:客户端已确认可以发货");55                 SendGoods();56                 if (_latPassNotice != null)57                 {58                     _latPassNotice(NoticeContext);59                 }60                 if (NoticeContext.IsAccept)61                 {62                     Console.WriteLine("服务端:客户端已收货");63                 }64             }65             else66             {67                 Console.WriteLine("服务端:等待客户端确认");68             }69 70         }71     }
View Code

Order类代表着订单(这里这个比喻有点不恰当),里面有着两个PassNotice委托类型的事件BefPassNotice、LatPassNotice,分别表示订单发货前的验证和发货后的客户可针对的操作(对应着HttpApplication中的各种事件),再看一下客户类

代码1-2

 1     public class NoticeContext 2     { 3         public bool IsSend { get; set; } 4         public bool IsAccept { get; set; } 5     } 6  7     public class Customer 8     { 9         private Order _Order;10         public Customer(Order order)11         {12             _Order = order;13             _Order.BefPassNotice += new PassNotice(_Order_BefPassNotice);14             _Order.LatPassNotice += new PassNotice(_Order_LatPassNotice);15         }16 17         void _Order_LatPassNotice(NoticeContext noticeContext)18         {19             noticeContext.IsAccept = true;20             Console.WriteLine("客户端:接收货物");21         }22 23         void _Order_BefPassNotice(NoticeContext noticeContext)24         {25 26             noticeContext.IsSend = true;27             Console.WriteLine("客户端:可以发货");28         }29     }
View Code

这里Customer类有着对Order类的引用,并且订阅Order类的事件,从而可以处理到Order类中的NoticeContex类型值(类似于HttpModule)。

调用代码1-3

1             Order order = new Order();2             Customer customer = new Customer(order);3             order.NoticeContext = new NoticeContext() { IsAccept = false, IsSend = false };4             order.Start();

结果如图2所示图2

示例中还有很多地方没有说明白,只演示了一个大概的模型,还有要说的就是Order类型对应着的处理是单一的(示例中欠妥),就好比HttpApplication只能对应着一个请求一样,当前的上下文信息中也只是存放着当前请求的信息。在代码1-3中对Customer是直接的调用的,而在ASP.NET中不是这样的。

下一篇将会讲到MVC中的路由部分,对于这个学前篇系列慢慢来完善吧。

作者:金源

出处:http://www.cnblogs.com/jin-yuan/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面