国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET MVC學前篇之請求流程

2019-11-17 01:34:26
字體:
來源:轉載
供稿:網友

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.survivalescaperooms.com/jin-yuan/

本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 峨眉山市| 绿春县| 多伦县| 武夷山市| 游戏| 蒲江县| 烟台市| 北辰区| 伊春市| 惠东县| 南涧| 抚远县| 阿尔山市| 太湖县| 乐平市| 黄山市| 罗甸县| 长沙县| 新田县| 白银市| 西和县| 高唐县| 鹿泉市| 玉林市| 延庆县| 桐城市| 从化市| 缙云县| 北碚区| 三原县| 云龙县| 克什克腾旗| 北京市| 五原县| 山西省| 元谋县| 绥化市| 平原县| 井冈山市| 霍城县| 隆化县|