ASP.NET 是一個(gè)開(kāi)發(fā)框架,用于通過(guò) HTML、CSS、JavaScript 以及服務(wù)器腳本來(lái)構(gòu)建網(wǎng)頁(yè)和網(wǎng)站。
MVC 是三個(gè) ASP.NET 開(kāi)發(fā)模型之一。
MVC 是用于構(gòu)建 web 應(yīng)用程序的一種框架,使用 MVC (Model View Controller) 設(shè)計(jì):
Model(模型)表示應(yīng)用程序核心(比如數(shù)據(jù)庫(kù)記錄列表)
View(視圖)對(duì)數(shù)據(jù)(數(shù)據(jù)庫(kù)記錄)進(jìn)行顯示
Controller(控制器)處理輸入(寫(xiě)入數(shù)據(jù)庫(kù)記錄)
MVC 模型同時(shí)提供對(duì) HTML、CSS 以及 JavaScript 的完整控制。
MVC 模型通過(guò)三個(gè)邏輯層來(lái)定義 web 應(yīng)用程序:
business layer(業(yè)務(wù)層、模型邏輯)
display layer(顯示層、視圖邏輯)
input control(輸入控件、控制器邏輯)
AspNet MVC中比較重要的上下文,有如下:
核心的上下文有HttpContext(請(qǐng)求上下文),ControllerContext(控制器上下文)
過(guò)濾器有關(guān)有五個(gè)的上下文ActionExecutingContext,ActionExecutedContext,ResultExecutingContext,ResultExecutedContext,ExceptionContext
視圖相關(guān)的上下文ViewContext
這些上下文之間的關(guān)系如下圖所示
AspNet MVC是什么? 三聯(lián)
說(shuō)明:
1、ControllerContext是對(duì)HttpContext的封裝
2、過(guò)濾器等f(wàn)ilterContext上下文都是繼承自ControllerContext
3、ViewContext也是繼承自ControllerContext,同時(shí)封裝了對(duì)視圖的對(duì)象
由此可以看出,最基礎(chǔ)還是Aspnet的HttpContext上下文貫穿整個(gè)請(qǐng)求/應(yīng)答的,而Mvc是將HttpContext進(jìn)行再次封裝成ControllerContext。所以先看明白HttpContext與ControllerContext的來(lái)龍去脈即可大致了解這些上下文。
1、HttpContext的由來(lái)
先看看園里大叔的一張圖,如下所示。
大致的流程如下
AppManagerAppDomainFactory類實(shí)現(xiàn)IAppManagerAppDomainFactory接口的Create方法,內(nèi)部實(shí)現(xiàn)了創(chuàng)建AppDomain【HttpRuntime、HttpContext等都依附在AppDomain】、HostingEnvironment等一系列操作,并且得到一個(gè)ISAPIRuntime。
當(dāng)IIS接受一個(gè)請(qǐng)求就可以通過(guò)上一步所得到的ISAPIRuntime的ProcessRequest進(jìn)行處理請(qǐng)求。其間
①必須對(duì)WorkRequest針對(duì)不同的IIS版本進(jìn)行包裝,從而創(chuàng)建得到ISAPIWorkerRequest實(shí)例對(duì)象
②HttpRuntime調(diào)用ProcessRequestNoDemand處理上面所得到的WorkRequest,并且通過(guò)ProcessRequestInternal 實(shí)例化化請(qǐng)求的上下文,如下代碼所示
1、HttpContext context = new HttpContext(wr/WorkRequest*/, false /* initResponseWriter */);
③HttpContext的構(gòu)造函數(shù)內(nèi)部也初始化HttpRequest以及HttpResponse
具體的內(nèi)部細(xì)節(jié),可以猛戳這里去看大叔深入剖析
2、ControllerContext
ControllerContext在ControllerBase的Initialize方法內(nèi)部被實(shí)例化,ControllerBase作為基類,被后期控制器所繼承。ControllerContext也將作為其他的過(guò)濾器上下文的基類。
protected virtual void Initialize(RequestContext requestContext) {
ControllerContext = new ControllerContext(requestContext, this);
}
public RequestContext RequestContext {
get {
if (_requestContext == null) {
// still need explicit calls to constructors since the property getters are virtual and might return null
HttpContextBase httpContext = HttpContext ?? new EmptyHttpContext();
RouteData routeData = RouteData ?? new RouteData();
_requestContext = new RequestContext(httpContext, routeData);
}
return _requestContext;
}
set {
_requestContext = value;
}
}
3、過(guò)濾器上下文
過(guò)濾器采用AOP(面向切面編程),可以通過(guò)實(shí)現(xiàn)IActionFilter,IResultFilter,IExceptionFilter,IAuthorizationFilter接口,進(jìn)行附加的過(guò)濾效果。這些接口的內(nèi)部方法的參數(shù)有相對(duì)應(yīng)的上下文,如IActionFilter內(nèi)部含有ActionExecutingContext,ActionExecutedContext上下文,而且將ControllerActionInvoker的InvokeActionMethodWithFilters內(nèi)部被實(shí)例化
public interface IActionFilter {
void OnActionExecuting(ActionExecutingContext filterContext);
void OnActionExecuted(ActionExecutedContext filterContext);
}
protected virtual ActionExecutedContext InvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters) {
ActionExecutingContext preContext = new ActionExecutingContext(controllerContext, actionDescriptor, parameters);
//省略
}
4 視圖上下文
視圖上下文被實(shí)例化三個(gè)地方:ViewResultBase,HttpHelper、TemplateHelpers,該上下文更多的為渲染視圖提供數(shù)據(jù)支持
致此,基本介紹了MVC內(nèi)部的上下文內(nèi)容。
更多信息請(qǐng)查看IT技術(shù)專欄