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 BefPassNotice 15 { 16 add 17 { 18 _befPassNotice += value; 19 } 20 remove 21 { 22 _befPassNotice -= value; 23 } 24 } 25 26 private PassNotice _latPassNotice; 27 28 public event PassNotice LatPassNotice 29 { 30 add 31 { 32 _latPassNotice += value; 33 } 34 remove 35 { 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 else 66 { 67 Console.WriteLine("服務端:等待客戶端確認"); 68 } 69 70 } 71 }
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 }
這里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/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面
