上一篇 说到了路由事件注册以及路由表的生成, 前面 也解析到了, 管道事件的建立, 那么接下来, 肯定就是要调用执行这些事件了, 这些就不表了, 我已经得到我想要的部分了, 接下来, 在执行这些管道事件的时候, 肯定就会执行到之前 UrlRoutingModule注册的方法. 接下来, 就看一下, 这个事件干了些什么.

一、OnApplicationPostResolveRequestCache 方法

private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication) sender;
HttpContextBase context = new HttpContextWrapper(application.Context);
this.PostResolveRequestCache(context);
}

这里调用了自己(UrlRoutingModule)的 PostResolveRequestCache 方法.

public virtual void PostResolveRequestCache(HttpContextBase context)
{
   //根据http上下文去 之前生成的路由表中查找匹配的路由
RouteData routeData = this.RouteCollection.GetRouteData(context);
if (routeData != null)
{
     //这里返回的 IRouteHandler 其实就是 MvcRouteHandler(上一篇)有提到过
     //这个MvcRouteHandler中, 就有一个方法, 用来返回 IHttpHander的, 其实就是返回的 MvcHandler
IRouteHandler routeHandler = routeData.RouteHandler;
if (routeHandler == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
           SR.GetString("UrlRoutingModule_NoRouteHandler"), new object[0]));
}
     //在注册路由的时候, Ignore方式注册的, 会返回StopRoutingHandler
if (!(routeHandler is StopRoutingHandler))
{
       //在这里, 首先创建了请求上下文
RequestContext requestContext = new RequestContext(context, routeData);
context.Request.RequestContext = requestContext;
        //在这里获取到MVC的处理接口, 就是 MvcHandler
IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
if (httpHandler == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture,
             SR.GetString("UrlRoutingModule_NoHttpHandler"), new object[] { routeHandler.GetType() }));
}
if (httpHandler is UrlAuthFailureHandler)
{
if (!FormsAuthenticationModule.FormsAuthRequired)
{
throw new HttpException(0x191, SR.GetString("Assess_Denied_Description3"));
}
UrlAuthorizationModule.ReportUrlAuthorizationFailure(HttpContext.Current, this);
}
else
{
          //将 MvcHandler 存入 HttpContext 对象的 _remapHandler 属性中
context.RemapHandler(httpHandler);
}
}
}
}

按照顺序, 看上面我标红的接个方法.

1. GetRouteData()方法

//RouteCollection
public RouteData GetRouteData(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
if (httpContext.Request == null)
{
throw new ArgumentException(SR.GetString("RouteTable_ContextMissingRequest"), "httpContext");
}
if (base.Count != )
{
bool flag = false;
bool flag2 = false;
if (!this.RouteExistingFiles)
{
flag = this.IsRouteToExistingFile(httpContext);
flag2 = true;
if (flag)
{
return null;
}
}
using (this.GetReadLock())
{
foreach (RouteBase base2 in this)
{
RouteData routeData = base2.GetRouteData(httpContext);
if (routeData != null)
{
if (!base2.RouteExistingFiles)
{
if (!flag2)
{
flag = this.IsRouteToExistingFile(httpContext);
flag2 = true;
}
if (flag)
{
return null;
}
}
return routeData;
}
}
}
}
return null;
}

接着看, 这里调用了 Route 类的 GetRouteData() 方法

public override RouteData GetRouteData(HttpContextBase httpContext)
{
string virtualPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring() + httpContext.Request.PathInfo;
RouteValueDictionary values = this._parsedRoute.Match(virtualPath, this.Defaults);
if (values == null)
{
return null;
}
RouteData data = new RouteData(this, this.RouteHandler);
if (!this.ProcessConstraints(httpContext, values, RouteDirection.IncomingRequest))
{
return null;
}
foreach (KeyValuePair<string, object> pair in values)
{
data.Values.Add(pair.Key, pair.Value);
}
if (this.DataTokens != null)
{
foreach (KeyValuePair<string, object> pair2 in this.DataTokens)
{
data.DataTokens[pair2.Key] = pair2.Value;
}
}
return data;
}

这里就会根据之前注册路由的限制条件去匹配路由了. 将最后匹配到的那个路由返回.

2. GetHttpHandler()方法

这里会调用 MvcRouteHandler 的 GetHttpHandler 方法, 前面篇章提过的.

protected virtual IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.HttpContext.SetSessionStateBehavior(this.GetSessionStateBehavior(requestContext));
return new MvcHandler(requestContext);
}

返回一个 MvcHandler .

3. RemapHandler()方法

这里会调用 HttpContext 的RemapHandler 方法.

public void RemapHandler(IHttpHandler handler)
{
this.EnsureHasNotTransitionedToWebSocket();
IIS7WorkerRequest request = this._wr as IIS7WorkerRequest;
if (request != null)
{
if (this._notificationContext.CurrentNotification >= RequestNotification.MapRequestHandler)
{
throw new InvalidOperationException(SR.GetString("Invoke_before_pipeline_event",
         new object[] { "HttpContext.RemapHandler", "HttpApplication.MapRequestHandler" }));
}
string handlerType = null;
string handlerName = null;
if (handler != null)
{
Type type = handler.GetType();
handlerType = type.AssemblyQualifiedName;
handlerName = type.FullName;
}
request.SetRemapHandler(handlerType, handlerName);
}
this._remapHandler = handler;
}

这里主要就是看最后一句了, 将 mvchandler存入到 HttpContext._remapHandler 中

到这里, 其实就能看到 路由匹配的过程以及获取到 mvc处理程序(这个是入口).

二、路由匹配

在分析路由的时候, 可以借助一个插件 : RouteDebugger, 获取方式为, 在vs程序包控制台中, 执行以下命令

PM> Install-Package routedebugger

添加完之后, 程序会自动在web.config中添加如下配置:

<add key="RouteDebugger:Enabled" value="true" />

我们只需要直接运行程序就好了. 先看一张效果图:

在图上, 能清晰的看到, 匹配过程中, 与其中的哪一个路由能匹配的上. 哪一些不能匹配上.

两个小问题:

  如果有两个匹配规则相同, 但是路由名不同的, 会匹配上哪一个呢?

    哪一个在前面注册的, 就回匹配到哪一个, 后面的不会继续匹配.

  那会不会有匹配规则不同, 但是路由名相同的呢?

    一般在使用key的时候, 都是要保证key的唯一性, 路由同样如此, 路由名不能重复, 必须唯一, 否则会报错.

有兴趣的朋友, 可以去了解下

目录已同步

MVC源码分析 - 路由匹配的更多相关文章

  1. ASP.NET MVC源码分析

    MVC4 源码分析(Visual studio 2012/2013) HttpModule中重要的UrlRoutingModule 9:this.OnApplicationPostResolveReq ...

  2. ASP.NET MVC 源码分析(一)

    ASP.NET MVC 源码分析(一) 直接上图: 我们先来看Core的设计: 从项目结构来看,asp.net.mvc.core有以下目录: ActionConstraints:action限制相关 ...

  3. 精尽Spring MVC源码分析 - WebApplicationContext 容器的初始化

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  4. 精尽Spring MVC源码分析 - HandlerMapping 组件(一)之 AbstractHandlerMapping

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  5. 精尽Spring MVC源码分析 - HandlerMapping 组件(二)之 HandlerInterceptor 拦截器

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  6. 精尽Spring MVC源码分析 - HandlerMapping 组件(三)之 AbstractHandlerMethodMapping

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  7. 精尽Spring MVC源码分析 - HandlerMapping 组件(四)之 AbstractUrlHandlerMapping

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  8. 精尽Spring MVC源码分析 - HandlerExceptionResolver 组件

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  9. 精尽Spring MVC源码分析 - LocaleResolver 组件

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

随机推荐

  1. 安装Windows2003操作系统 - 初学者系列 - 学习者系列文章

    Windows 2003是一款经典的服务器操作系统.以前笔者工作的时候就是用的这款操作系统来进行编程的.下面就对该操作系统的安装进行介绍(部分步骤参见XP的安装http://www.cnblogs.c ...

  2. 使用Windows2003的IIS发布网站 - 进阶者系列 - 学习者系列文章

    本系列文章目录:http://www.cnblogs.com/lzhdim/category/277150.html Windows 2003是一款不错的Windows服务器版本.在刚工作的前些年里, ...

  3. JBoss7官方下载最新版本

    JBoss是全世界开发人员共同努力的成果.一个基于J2EE的开放源码的应用server. 由于JBoss代码遵循LGPL许可,能够在不论什么商业应用中免费使用它.而不用支付费用. 2006年,Jbos ...

  4. Extension+NVelocity

    VS Extension+NVelocity系列(二)——让VS支持 NVelocity的智能提示(上)   一.基础概念 应该庆幸的是,VS的插件是靠着MEF实现而不是MAF,这让你所做的工作减轻了 ...

  5. Web层后端权限模块

    从零开始编写自己的C#框架(19)——Web层后端权限模块   不知不觉本系统写了快三个月了,最近写页面的具体功能时感觉到有点吃力,很多地方如果张嘴来讲的话可以说得很细,很全面,可写成文字的话,就不太 ...

  6. Spire.Office for .NET(Word、Excel、PPT、PDF等)

    使用Spire.Office for .NET(Word.Excel.PPT.PDF等)的初步感受 前言 本文大部分内容来自http://www.codeproject.com/Articles/71 ...

  7. WebService和AngularJS实现模糊过滤查询

    WebService和AngularJS实现模糊过滤查询   [概要] 网上看到一个不错的帖子,用WebService获取json,然后在前端使用AngularJs进行过滤搜索,看完文章后,按自己的想 ...

  8. 13个不可不知的ASP.NET MVC扩展点

    13个不可不知的ASP.NET MVC扩展点 ASP.NET MVC设计的主要原则之一是可扩展性.处理管线(processing pipeline)上的所有(或大多数)东西都是可替换的.因此,如果您不 ...

  9. Web Components

    Web Components是不是Web的未来   今天 ,Web 组件已经从本质上改变了HTML.初次接触时,它看起来像一个全新的技术.Web组件最初的目的是使开发人员拥有扩展浏览器标签的能力,可以 ...

  10. 【转】几点 iOS 开发技巧

    [译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...