🚫 Ad Blocker Detected

Please disable your AD blocker to continue using this site. Ads help us keep the content free! please press keyboard F5 to refresh page after disabled AD blocker

請關閉廣告攔截器以繼續使用本網站。廣告有助於我們保證內容免費。謝謝! 關閉後請按 F5 刷新頁面

0%

Agenda

前言

UrlRoutingModule對於OnPostResolveRequestCache事件添加一個對於MVC很重要的動作,透過RouteCollection取得此次請求匹配RouteData物件.

利用此RouteData取得要使用的IHttpHandler來執行它.

1
RouteData routeData = RouteCollection.GetRouteData(context);

RouteCollection是全域路由註冊表.我們在一開始使用MapRoute註冊與之匹配ControllerAction

RouteCollection是基於RouteBase物件集合,所以它可以存放所有繼承RouteBase物件,RouteBase這個類別有一個重要的方法來取得RouteData,RouteData封裝此次Http請求的Controller,Action…等資訊

對於每個Http請求依序找尋第一個匹配路由規則

1
2
3
4
5
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Read more »

Agenda

前言

上一篇說到最終會透過一個實現IView物件(Razor是透過RazorView)來完成,RenderView方法將BuildManagerCompiledView方法取得物件轉換型別成WebViewPage.

.cshtml最終會編譯成一個繼承WebViewPage檔案.

本篇會來解析View編譯原理

我有做一個可以針對於Asp.net MVC Debugger的專案,只要下中斷點就可輕易進入Asp.net MVC原始碼.

WebViewPage

WebViewPage繼承樹最頂層有個WebPageExecutingBase抽象類別,他擁有一個抽象方法Execute,View轉成c#程式會建立一個類別就會繼承於WebViewPage並把使用者頁面程式碼實現在Execute方法.

1
public abstract void Execute();
Read more »

Agenda

前言

上一篇說到最終會透過一個實現IView物件(Razor是透過RazorView)來完成,RenderView方法將BuildManagerCompiledView方法取得物件轉換型別成WebViewPage.

.cshtml最終會編譯成一個繼承WebViewPage檔案.

本篇會來解析View編譯原理

我有做一個可以針對於Asp.net MVC Debugger的專案,只要下中斷點就可輕易進入Asp.net MVC原始碼.

WebViewPage

WebViewPage繼承樹最頂層有個WebPageExecutingBase抽象類別,他擁有一個抽象方法Execute,View轉成c#程式會建立一個類別就會繼承於WebViewPage並把使用者頁面程式碼實現在Execute方法.

1
public abstract void Execute();
Read more »

Agenda

前言

繼承ActiontResult類別中ViewResultBase最為複雜,因為ViewResultBase要找到實現IViewEngine物件取得取得View檔案,在透過實現IView物件把頁面渲染出來.

這篇會跟大家分享值型上面動作核心類別.

個人覺得MVC運用很多物件導向概念和用法,在讀程式時有件事情很重要是理解類別負責的工作和類別之間關係.就像現實生活中人與人的關係要了解清楚.

我有做一個可以針對於Asp.net MVC Debugger的專案,只要下中斷點就可輕易進入Asp.net MVC原始碼.

ViewResultBase.ExecuteResult

因為ExecuteResult是最終被呼叫方法,我們來解析ViewResultBase.ExecuteResult方法邏輯.

  1. 透過子類別實現FindView取得View相關資料.
  2. 呼叫實現IView物件Render方法,並將渲染出來資料透過Response.Output輸出到Client
Read more »

Agenda

前言

繼承ActiontResult類別中ViewResultBase最為複雜,因為ViewResultBase要找到實現IViewEngine物件取得取得View檔案,在透過實現IView物件把頁面渲染出來.

這篇會跟大家分享值型上面動作核心類別.

個人覺得MVC運用很多物件導向概念和用法,在讀程式時有件事情很重要是理解類別負責的工作和類別之間關係.就像現實生活中人與人的關係要了解清楚.

我有做一個可以針對於Asp.net MVC Debugger的專案,只要下中斷點就可輕易進入Asp.net MVC原始碼.

ViewResultBase.ExecuteResult

因為ExecuteResult是最終被呼叫方法,我們來解析ViewResultBase.ExecuteResult方法邏輯.

  1. 透過子類別實現FindView取得View相關資料.
  2. 呼叫實現IView物件Render方法,並將渲染出來資料透過Response.Output輸出到Client
Read more »

Agenda

前言

上一篇介紹到CreateActionResult方法會產生一個ActionResult物件利用MethodInfo資訊.

最後透過InvokeActionResult來呼叫ExecuteResult方法來執行ActionResultExecuteResult方法,基本上MVC找到且執行Action方法後面就沒再做甚麼特別的事情了(後面做資源釋放…)

1
2
3
4
protected virtual void InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
{
actionResult.ExecuteResult(controllerContext);
}

本篇來介紹常用的ActionResult其內部運作程式碼

我有做一個可以針對於Asp.net MVC Debugger的專案,只要下中斷點就可輕易進入Asp.net MVC原始碼.

6種基本的ActionResult

下面這六個類別是直接繼承於ActionResult的類別(其中有標註Base class代表這是抽象類別另外有類別繼承它)

Read more »

Agenda

前言

上一篇介紹到CreateActionResult方法會產生一個ActionResult物件利用MethodInfo資訊.

最後透過InvokeActionResult來呼叫ExecuteResult方法來執行ActionResultExecuteResult方法,基本上MVC找到且執行Action方法後面就沒再做甚麼特別的事情了(後面做資源釋放…)

1
2
3
4
protected virtual void InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
{
actionResult.ExecuteResult(controllerContext);
}

本篇來介紹常用的ActionResult其內部運作程式碼

我有做一個可以針對於Asp.net MVC Debugger的專案,只要下中斷點就可輕易進入Asp.net MVC原始碼.

6種基本的ActionResult

下面這六個類別是直接繼承於ActionResult的類別(其中有標註Base class代表這是抽象類別另外有類別繼承它)

Read more »

Agenda

前言

不知道大家有沒有點暈頭轉向XD,MVCModel綁定機制真的蠻複雜,希望大家有跟上來

透過DefaultModelBinderBindComplexElementalModel方法綁定複雜模型的值.

BindProperty方法時填充子節點ModelMetadataModel屬性,透過(DefaultModelBinder)再次綁定物件動作如下

  • ModelMetadata是簡單模型就會把值填充給此次ModelMetadata.Model
  • ModelMetadata是複雜模型就建立一個物件後呼叫BindProperty直到找到最後的簡單模型.

BindComplexElementalModel方法做幾個主要動作

  1. BindProperties:透過MetaData取得屬性資訊並利用反射把值添加上去.
  2. OnModelUpdated:找尋ModelMetaDataModelValidator進行屬性驗證,如果驗證失敗會把資料資訊加到ModelState.AddModelError(ModelStateDictionary)可在View搭配顯示error訊息
1
2
3
4
5
6
7
8
9
10
internal void BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, object model)
{
ModelBindingContext newBindingContext = CreateComplexElementalModelBindingContext(controllerContext, bindingContext, model);

if (OnModelUpdating(controllerContext, newBindingContext))
{
BindProperties(controllerContext, newBindingContext);
OnModelUpdated(controllerContext, newBindingContext);
}
}
Read more »

Agenda

前言

不知道大家有沒有點暈頭轉向XD,MVCModel綁定機制真的蠻複雜,希望大家有跟上來

透過DefaultModelBinderBindComplexElementalModel方法綁定複雜模型的值.

BindProperty方法時填充子節點ModelMetadataModel屬性,透過(DefaultModelBinder)再次綁定物件動作如下

  • ModelMetadata是簡單模型就會把值填充給此次ModelMetadata.Model
  • ModelMetadata是複雜模型就建立一個物件後呼叫BindProperty直到找到最後的簡單模型.

BindComplexElementalModel方法做幾個主要動作

  1. BindProperties:透過MetaData取得屬性資訊並利用反射把值添加上去.
  2. OnModelUpdated:找尋ModelMetaDataModelValidator進行屬性驗證,如果驗證失敗會把資料資訊加到ModelState.AddModelError(ModelStateDictionary)可在View搭配顯示error訊息
1
2
3
4
5
6
7
8
9
10
internal void BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, object model)
{
ModelBindingContext newBindingContext = CreateComplexElementalModelBindingContext(controllerContext, bindingContext, model);

if (OnModelUpdating(controllerContext, newBindingContext))
{
BindProperties(controllerContext, newBindingContext);
OnModelUpdated(controllerContext, newBindingContext);
}
}
Read more »

Agenda

前言

CachedDataAnnotationsMetadataAttributes這個類別攔截某些標籤可被攔截驗證.

本篇會介紹另一個可以客製化驗證ValidationAttribute,常用驗證標籤並講述是如何參數屬性是如何取得這個標籤和使用過程.

我有做一個可以針對於Asp.net MVC Debugger的專案,只要下中斷點就可輕易進入Asp.net MVC原始碼.

ValidationAttribute

ValidationAttribute類別在System.ComponentModel.DataAnnotations命名空間下.

我們可以建立一個類別繼承ValidationAttributebool IsValid(object value)重載方法來製做我們客制化驗證機制.

IsValid方法有一個Bool回傳值回傳true代表驗證通過false反之

Read more »