当前位置:编程学习 > asp >>

技术原理解析[3:我记录框架处理流程]

要分析我记录的整个处理流程,先从wojilu的初始化入口,看看wojilu在初始化的时候到底做了些什么事情。

相关代码:wojilu.WebGlobal.asax

 

1 void Application_Start( object sender, EventArgs e ) {
2         wojilu.Web.SystemInfo.Init();
3         wojilu.Web.Mvc.MvcFilterLoader.Init();
4         wojilu.Web.Jobs.WebJobStarter.Init();
5     }
wojilu.Web.SystemInfo:系统的固定数据,比如网站根路径、app路径、主机(ip+端口号)名称等。这里的信息是以后路由的参考信息,在以后URL和实际地址映射的时候将用到这里的数据。

wojilu.Web.Mvc.MvcFilterLoader: mvc 过滤器的加载器。初始化各种过滤器,并且将各种过滤器注册到系统之中。

wojilu.Web.Jobs.WebJobStarter:计划任务启动器的初始化。

WebJob:计划任务,类似于服务的一种任务,可以指定Interval(间隔时间)。

通过上面这些步骤,整个系统就可以响应URL请求了。

 

接下来就是介绍一下整个MVC的流程:

 \

一个标准的MVC流程是指从路由解析(Route)到页面生成(Render)的全过程。

这些原本都是由ASP完成的任务,现在全都由wojilu系统完成了。

在详细介绍各个Process之前,我们先看看ProcessorBase的定义吧:

 

 1 namespace wojilu.Web.Mvc.Processors {
 2
 3     internal abstract class ProcessorBase {
 4
 5
 6         public abstract void Process( ProcessContext context );
 7
 8     }
 9
10 }
ProcessorBase 是一个抽象类,里面有一个抽象的方法Process(处理),这个方法的参数是 ProcessContext(处理内容)。ProcessContext里面包含着生成页面所需要的一切信息,从路由解析开始,数据从URL信息开始,通过各个Process一点点变得丰富。例如通过路由的时候,URL将被解释成页面Controller信息放入ProcessContext中,通过安全认证Process的时候,访问权限等信息会被追加到ProcessContext中。ProcessContext作为信息的载体,贯穿于整个Process中。

了解了整个MVC流程之后,我们要问,触发MVC的最初的地方在哪里。

我们知道,要自定义HTTP处理,就必须自己实现一个 IHttpHandler,我记录系统要实现全部的HTTP访问过程,必然需要实现IHttpHandler。所以wojilu的IHttpHandler实现就是整个MVC的源头。

 

 1 /*
 2  * Copyright 2010 www.wojilu.com
 3  *
 4  * Licensed under the Apache License, Version 2.0 (the "License");
 5  * you may not use this file except in compliance with the License.
 6  * You may obtain a copy of the License at
 7  *
 8  *      

 9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Web;
19 using System.Web.SessionState;
20
21 using wojilu.Web.Context;
22 using wojilu.Caching;
23 using System.Text;
24 using System.Collections.Generic;
25
26 namespace wojilu.Web.Mvc {
27     /// <summary>
28     /// wojilu mvc 的核心处理器:处理客户端请求,将结果返回
29     /// </summary>
30     public class CoreHandler : IHttpHandler, IRequiresSessionState {
31         public virtual void ProcessRequest( HttpContext context ) {
32             if (MvcConfig.Instance.IsPageCache) {
33                 String pageContent = CacheManager.GetApplicationCache().Get( context.Request.Url.PathAndQuery ) as String;
34                 if (pageContent != null) {
35                     context.Response.Write( pageContent );
36                     return;
37                 }
38             }
39             MvcContext ctx = new MvcContext( new WebContext( context ) );
40             ProcessContext.Begin( ctx );
41         }
42         public virtual void ProcessRequest( IWebContext context ) {
43             ProcessContext.Begin( new MvcContext( context ) );
44         }
45         public Boolean IsReusable {
46             get { return true; }
47         }
48     }
49 }
 看到这个,大家不禁要问,系统怎么知道HTTP使用哪个IHttpHandler的具体实现呢?

接下来请大家打开 源代码 wojilu.Web 的 Web.Config文件。

这里有一个httpHandlers节:

里面定义了对于各种请求的处理方法。例如,对于refresh.aspx使用wojilu.Web.Handler.RefreshServerHandler来处理,robots.txt使用System.Web.DefaultHttpHandler来处理。

最后一行里,如果这个请求不属于其他处理方法的职责范围的话(例如aspx),就使用wojilu.Web.Mvc.CoreHandler来处理。

1     <httpHandlers>
2         <add verb="*" path="CaptchaImage.ashx" type="wojilu.Web.Handler.CaptchaImageHandler, wojilu"/>    
3         <!-- <add verb="*" path="WebForm1.aspx" type="System.Web.UI.PageHandlerFactory" /> -->
4         <add verb="*" path="*.asp" type="wojilu.Web.Handler.PageNotFoundHandler, wojilu"/>
5         <add verb="*" path="robots.txt" type="System.Web.DefaultHttpHandler"/>
6         <add verb="*" path="refresh.aspx" type="wojilu.Web.Handler.RefreshServerHandler, wojilu.Core"/>
7         <add verb="*" path="*.css,*.jpg,*.jpeg,*.gif,*.png,*.bmp,*.ico,*.js,*.htm,*.html,*.xml,*.swf,*.zip,*.7z,*.rar,*.cur" type="System.Web.DefaultHttpHandler"/>
8         <add verb="*" path="*" type="wojilu.Web.Mvc.CoreHandler, wojilu"/>
9     </httpHandlers>
这样的话,对于aspx的页面请求,通过查看Config知道了使用wojilu.Web.Mvc.CoreHandler来处理。通过调用wojilu.Web.Mvc.CoreHandler的ProcessRequest方法,开始处理请求,ProcessRequest的ProcessContext.Begin正式开始MVC之旅了。

Config ->

    wojilu.Web.Mvc.CoreHandler.ProcessRequest - >

          wojilu.Web.Mvc.CoreHandler.ProcessRequest: ProcessContext.Begin

这里介绍了整个MVC的大体流程和进入MVC的入口方法,大家学习wojilu可以从这里作为入口。

 

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,