详解ASP.NET MVC的整个生命周期(7)

namespace System.Web.Mvc { /// <summary>Defines the methods that are required for a result filter.</summary> public interface IResultFilter { /// <summary>Called before an action result executes.</summary> /// <param>The filter context.</param> void OnResultExecuting(ResultExecutingContext filterContext); /// <summary>Called after an action result executes.</summary> /// <param>The filter context.</param> void OnResultExecuted(ResultExecutedContext filterContext); } }

B、ActionResult 就是把处理的用户请求结果返回。因此 ViewResult, PartialViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult and EmptyResult就是具体的返回类型。

C、上面的返回类型可以大致分为2类:ViewResult 和非ViewResult。对于需要生成html页面给客户端的划到ViewResult,而其他的例如返回文本,json数据等则划分到非ViewResult,对于非ViewResult直接返回就可以了。

View的初始化和渲染呈现

A、对于 ViewResult 最终是由合适的 View Engine 通过调用 IView 的 Render() 方法来渲染的:

namespace System.Web.Mvc { /// <summary>Defines the methods that are required for a view engine.</summary> public interface IViewEngine { /// <summary>Finds the specified partial view by using the specified controller context.</summary> /// <returns>The partial view.</returns> /// <param>The controller context.</param> /// <param>The name of the partial view.</param> /// <param>true to specify that the view engine returns the cached view, if a cached view exists; otherwise, false.</param> ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache); /// <summary>Finds the specified view by using the specified controller context.</summary> /// <returns>The page view.</returns> /// <param>The controller context.</param> /// <param>The name of the view.</param> /// <param>The name of the master.</param> /// <param>true to specify that the view engine returns the cached view, if a cached view exists; otherwise, false.</param> ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache); /// <summary>Releases the specified view by using the specified controller context.</summary> /// <param>The controller context.</param> /// <param>The view.</param> void ReleaseView(ControllerContext controllerContext, IView view); } }

namespace System.Web.Mvc { /// <summary>Defines the methods that are required for a view.</summary> public interface IView { /// <summary>Renders the specified view context by using the specified the writer object.</summary> /// <param>The view context.</param> /// <param>The writer object.</param> void Render(ViewContext viewContext, TextWriter writer); } }

B、整个处理过程是由 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()); }

C、最后,Html Helpers将帮我们生成 input 标签,基于AJAX的 form 等等。

(5)、作为总结,将每个节点主要的代码类贴出来。

这就是整个流程的代码节点,有些是同步执行,有些是异步执行,把握关键点,我这里只是谢了一个大概。

UrlRoutingModule-----RouteCollection.GetRouteData(context)----->IRouteHandler routeHandler = routeData.RouteHandler------》IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext)-----》context.RemapHandler(httpHandler)------->MvcHandler------->ProcessRequest()------>ProcessRequestInit()--------》IController------>controller.Execute(this.RequestContext)-------->ControllerActionInvoker------->InvoleAction()--------->InvoleActionMethod()------->InvoleActionReslt()

三、结束

今天就到这里了,东西虽然不多,但是也写了2个多小时。今天就算自己有学习了一边,大家一定要好好的把握这个流程,对于解决程序中的问题,扩展框架都有很大的好处。我们作为程序员的,应该要知道其一,也要知道其二。没事,看看源码,我们对框架和我们自己的代码有更深的了解。当然,这样做也是有代价的,需要更多的时间去支持,我相信我们的付出是值得。不忘初心,继续努力。老天不会辜负努力的人。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zysfgd.html