public override RouteData GetRouteData(HttpContextBase httpContext) { string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo; //结合默认值,匹配url RouteValueDictionary values = _parsedRoute.Match(requestPath, Defaults); if (values == null) { return null; } //包装成RouteData,这里为什么不放在if后面呢? RouteData routeData = new RouteData(this, RouteHandler); //匹配约束 if (!ProcessConstraints(httpContext, values, RouteDirection.IncomingRequest)) { return null; } //RouteData的Values和DataTokens都来自于Route foreach (var value in values) { routeData.Values.Add(value.Key, value.Value); } if (DataTokens != null) { foreach (var prop in DataTokens) { routeData.DataTokens[prop.Key] = prop.Value; } } return routeData; }
可以看到,Route对象的GetRouteData方法会匹配url模式,和检查约束条件,如何不符合会返回null。如果匹配,则new一个RouteData。
步骤二、获取IRouteHandler接口对象
上面创建RouteData,参数分别是当前Route对象和它的RouteHandler属性。RouteHandler是一个IRouteHandler,这是一个重要接口,它的定义如下:
public interface IRouteHandler { IHttpHandler GetHttpHandler(RequestContext requestContext); }
很明显,它是用于获取IHttpHandler的。那么Route对象的RouteHandler属性又是在哪里初始化的呢?我们回到开始的注册方法,routes.MapRoute,这个方法根据传递的参数创建一个Route对象,该方法的实现如下:
public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces) { //创建一个Route对象,它的IRouteHandler为MvcRouteHandler Route route = new Route(url, new MvcRouteHandler()) { Defaults = CreateRouteValueDictionary(defaults), Constraints = CreateRouteValueDictionary(constraints), DataTokens = new RouteValueDictionary() }; if ((namespaces != null) && (namespaces.Length > 0)) { route.DataTokens["Namespaces"] = namespaces; } //将Route注册到RouteCollection中 routes.Add(name, route); return route; }
在创建Route时,除了传递url模式外,还默认帮我们传递了一个MvcRouteHandler,它实现了IRouteHandler接口。
步骤三、获取IHttpHandler接口对象
有了MvcRouteHandler,就可以调用它的GetHttpHandler方法获取IHttpHandler了,该方法实现如下:
protected virtual IHttpHandler GetHttpHandler(RequestContext requestContext) { //设置session状态 requestContext.HttpContext.SetSessionStateBehavior(GetSessionStateBehavior(requestContext)); //返回一个实现了IHttpHandler的MvcHandler return new MvcHandler(requestContext); }
可以看到,它返回了一个MvcHandler,MvcHandler就实现了IHttpHandler接口。所以开头说的,请求本质都是交给HttpHandler的,其实MVC也是这样的,请求交给了MvcHandler处理。我们可以看MvcHandler定义和主要方法:
public class MvcHandler : IHttpAsyncHandler, IHttpHandler, IRequiresSessionState { protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state) { IController controller; IControllerFactory factory; //这个方法里会激活Controller对象 ProcessRequestInit(httpContext, out controller, out factory); IAsyncController asyncController = controller as IAsyncController; if (asyncController != null) { // asynchronous controller BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { try { //调用Controller的BeginExecute方法 return asyncController.BeginExecute(RequestContext, asyncCallback, asyncState); } catch { factory.ReleaseController(asyncController); throw; } }; EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) { try { asyncController.EndExecute(asyncResult); } finally { factory.ReleaseController(asyncController); } }; SynchronizationContext syncContext = SynchronizationContextUtil.GetSynchronizationContext(); AsyncCallback newCallback = AsyncUtil.WrapCallbackForSynchronizedExecution(callback, syncContext); return AsyncResultWrapper.Begin(newCallback, state, beginDelegate, endDelegate, _processRequestTag); } else { // synchronous controller Action action = delegate { try { controller.Execute(RequestContext); } finally { factory.ReleaseController(controller); } }; return AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag); } } }
可以看到,MvcHandler的任务就是激活Controller,并执行它的Execute方法。这个过程和Webform里的页面处理是很相似的,.aspx请求到来,会根据虚拟路径找到实现IHttpHandler的Page(类似于路由机制根据url模式找到MvcHandler),然后进入Page的页面周期(类似于Mvc的激活Controller,然后执行Action过程)。
四、总结
接下来,简单总结一下请求进入到MVC框架的过程:
1.添加路由对象Route到全局的RouteCollection,Route的IRouteHandler初始化为MvcRouteHandler。