ASP.NET MVC 里面其实是在原来的ASP.NET 基础之上,通过用UrlRoutingModule和MvcHttpHandler来替换了原来web Form处理方式的。

UrlRoutingModule 微软没有开源的,但是我们可以来分析下MvcHttpHandler

实现了:IHttpHandler接口

  #region IHttpHandler Members
        bool IHttpHandler.IsReusable {
            get {
                return IsReusable;
            }
        }

        void IHttpHandler.ProcessRequest(HttpContext httpContext) {
            ProcessRequest(httpContext); //有点像适配器模式
        }
        #endregion

//MVC真正处理请求的方法

  protected virtual void ProcessRequest(HttpContext httpContext) {
            HttpContextBase iHttpContext = new HttpContextWrapper(httpContext); //通过httpContext上下文,初始化一个MVC环境中的上下文
            ProcessRequest(iHttpContext);
        }

 

 protected internal virtual void ProcessRequest(HttpContextBase httpContext) {
            AddVersionHeader(httpContext);

            // Get the controller type
            string controllerName = RequestContext.RouteData.GetRequiredString("controller");  //   这个代码的意思就是根据当前上下文,找出controller类

            // Instantiate the controller and call Execute
            IControllerFactory factory = ControllerBuilder.GetControllerFactory();  //实例化一个controller类型工厂,运用了抽象工厂模式
            IController controller = factory.CreateController(RequestContext, controllerName);  //工厂方法
            if (controller == null) {
                throw new InvalidOperationException(
                    String.Format(
                        CultureInfo.CurrentUICulture,
                        MvcResources.ControllerBuilder_FactoryReturnedNull,
                        factory.GetType(),
                        controllerName));
            }
            try {
                controller.Execute(RequestContext);   //输入MVC上下文对象,处理逻辑,找到Model,选择View,最终实现请求,
            }
            finally {
                factory.ReleaseController(controller);  //释放资源
            }
        }

 

可以看到ASP.NET MVC 并不神秘,只是在MvcHandler 这个类里面做了一些处理,明白了这些,我们就可以自己在扩展一些东西,比如

自己在继承MvcHandler类。

 

至于MVC 的View 部分,其实就是另外一套 输入HTML的技术。

看看View的父类ViewPage : Page, IViewDataContainer  继承了这2个东西

在看看接口IViewDataContainer  :

ViewDataDictionary ViewData { get; set; }  从字面意识可以知道是 数据字典 ,也就是 View页面上展示的数据 就是来自于这个ViewDataDictionary

在看看 ViewDataDictionary  里面的代码,注意到有这么一个Dictionary<string, object> _innerDictionary 对象,内嵌泛型字典。

呵呵,现在我们就知道在View页面上怎么 绑定数据了,比如

 <h2><%= Html.Encode(ViewData["Message"]) %></h2>  这下就知道了 什么意思了撒,Message就是 数据的一个key值,要什么数据只需要晓得

他的Key值,便能知道他的数据。

 

呵呵,通过以上代码的 分析,大概你也明白了,ASP MVC是个什么样子的,你也许会认为这不过是微软 的一个骗术罢了,并没有什么高明的,

反而失去了,服务器控件的一些优点,我觉得总的来说 还是 缺点大与 优点。

 

 

 

 

 

 

posted on 2009-05-27 17:28  悲哀ing  阅读(1210)  评论(2编辑  收藏  举报