漫步ASP.NET MVC的處理管線

來源:互聯網
上載者:User

標籤:http   io   ar   使用   sp   for   資料   on   div   

ASP.NET MVC從誕生到現在已經好幾個年頭了,這個架構提供一種全新的開發模式,更符合web開發本質。你可以很好的使用以及個人化和擴充這個架構,但這需要你對它有足夠的瞭解。這篇文章主要從整體角度總結一下MVC的處理模型。

 整體處理模型 先放一張圖(圖最直觀而且很有說服力): ASP.NETMVC5Pipeline 下面開始解釋各個部分: 路由模組 1.在ASP.NET MVC處理管線中的第一站就是路由模組。當請求到達路由模組後,MVC架構就會根據Route Table中配置的路由模板來匹配當前請求以獲得對應的controller和action資訊。具體的匹配過程就是有UrlRoutingModule(System.Web.Routing.UrlRoutingModule)來實現的。 2.當ASP.NET MVC應用程式第一次啟動的時候,路由系統就會把我們註冊的路由規則(攔截哪些請求)加到Route Table中,一個應用程式套件組合含一個Route Table,在Global.asax中的Application_Start事件中被建立: 複製代碼public class RouteConfig {    public static void RegisterRoutes(RouteCollection routes) {   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    routes.MapRoute(    name: "Default",    url: "{controller}/{action}/{id}",    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }  protected void Application_Start(){  RouteConfig.RegisterRoutes(RouteTable.Routes);}複製代碼3.當UrlRoutingModule在Route Table中找到一條匹配的路由規則時,就會為這條路由規則尋找對應的IRouteHandler(System.Web.Mvc.IRouteHandler)執行個體(預設是System.Web.MvcRouteHandler),根據這個RouteHandler最後擷取一個IHttpHandler的執行個體(預設是System.Web.MvcHandler) public interface IRouteHandler{ IHttpHandler GetHttpHandler(RequestContext requestContext);}Controller初始化 1.在MvcHandler中的ProcessRequest方法中就是ASP.NET MVC的生命週期,這個方法使用IControllerFactory的執行個體(預設是System.Web.Mvc.DefaultControllerFactory)來建立相應的controller: 複製代碼protected internal virtual void ProcessRequest(HttpContextBase httpContext){ SecurityUtil.ProcessInApplicationTrust(delegate { IController controller; IControllerFactory factory; this.ProcessRequestInit(httpContext, out controller, out factory); try { controller.Execute(this.RequestContext); } finally { factory.ReleaseController(controller); } });}複製代碼Action的執行 1.當controller建立之後,緊接著就會執行自己的InvokeAction()方法: public virtual bool InvokeAction(ControllerContext controllerContext, string actionName)2.當選擇完合適的action後,接著就是model binders(預設是System.Web.Mvc.DefaultModelBinder),它會從http請求的參數中提取資料並實作類別型轉換,資料校正(例如是否必填,資料格式等)以及是否自動裝配到action方法的參數中System.Web.Mvc.DefaultModelBinder 3.Authentication Filter是mvc5中新增的一個Filter,它會先於authorization filter執行,目的是對訪問使用者的認證。在MVC5之前,認證和授權都是通過authorization filter來實現的,但現在這2個操作就分開來了,各自管各自嘍。 4.Action filters有2個方法OnActionExecuting和OnActionExecuted分別在action執行前後執行。我們也可以通過實現IActionFilter介面來實現你個人化的過濾機制 5.接下來就是執行我們平時在action方法中寫的代碼了(根據請求相應結果) ActionResult的執行 1.在ActionResult執行前後,仍然會有一個filter(ResultFilter),同樣的,通過實現IResultFilter介面你可以定製自己的過濾邏輯。 2.ActionResult就是把BAL DAL處理的使用者請求結果返回。因此ViewResult, PartialViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult and EmptyResult就是具體的傳回型別 3.上面的傳回型別可以大致分為2類:ViewResult和非ViewResult。對於需要產生html頁面給用戶端的划到ViewResult,而其他的例如返迴文本,json資料等則劃分到非ViewResult,對於非ViewResult直接返回就可以了。 View的初始化和渲染呈現 1.對於ViewResult最終是由合適的View Engine通過調用IView的Render()方法來渲染View的: public interface IView{ void Render(ViewContext viewContext, TextWriter writer);}2.整個處理過程是由IViewEngine(System.Web.Mvc.IViewEngine)來實現的。ASP.NET MVC 預設提供webform(.aspx)和Razor(.cshtml)模板引擎,你可以通過實現IViewEngine介面來實現你自己的ViewEngine,然後在Application_Start方法中做如下註冊: 複製代碼protected void Application_Start() {  //移除所有的View引擎包括Webform和Razor ViewEngines.Engines.Clear(); //註冊你自己的View引擎 ViewEngines.Engines.Add(new CustomViewEngine()); }複製代碼3.最後,Html Helpers將幫我們產生input標籤,基於AJAX的form等等。Html Helpers是HtmlHelper類的擴充方法,因此想要進一步擴充也是非常容易的。

漫步ASP.NET MVC的處理管線

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.