MVC源码分析 - 路由匹配
上一篇 说到了路由事件注册以及路由表的生成, 前面 也解析到了, 管道事件的建立, 那么接下来, 肯定就是要调用执行这些事件了, 这些就不表了, 我已经得到我想要的部分了, 接下来, 在执行这些管道事件的时候, 肯定就会执行到之前 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源码分析 - 路由匹配的更多相关文章
- ASP.NET MVC源码分析
MVC4 源码分析(Visual studio 2012/2013) HttpModule中重要的UrlRoutingModule 9:this.OnApplicationPostResolveReq ...
- ASP.NET MVC 源码分析(一)
ASP.NET MVC 源码分析(一) 直接上图: 我们先来看Core的设计: 从项目结构来看,asp.net.mvc.core有以下目录: ActionConstraints:action限制相关 ...
- 精尽Spring MVC源码分析 - WebApplicationContext 容器的初始化
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- 精尽Spring MVC源码分析 - HandlerMapping 组件(一)之 AbstractHandlerMapping
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- 精尽Spring MVC源码分析 - HandlerMapping 组件(二)之 HandlerInterceptor 拦截器
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- 精尽Spring MVC源码分析 - HandlerMapping 组件(三)之 AbstractHandlerMethodMapping
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- 精尽Spring MVC源码分析 - HandlerMapping 组件(四)之 AbstractUrlHandlerMapping
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- 精尽Spring MVC源码分析 - HandlerExceptionResolver 组件
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- 精尽Spring MVC源码分析 - LocaleResolver 组件
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
随机推荐
- php soap调用asp.net webservice
原文:php soap调用asp.net webservice 首先做一下准备工作,找到安装环境里的php.ini把;extension=php_soap.dll去掉前面的;.我这里使用的是wamp, ...
- web.xml在listener作用与用途
一.WebContextLoaderListener 监听类 它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作! 它必须在web.xml 中配置才干使用,是配置监听类的 二. ...
- Routing(路由) & Multiple Views(多个视图) step 7
Routing(路由) & Multiple Views(多个视图) step 7 1.切换分支到step7,并启动项目 git checkout step-7 npm start 2.需求: ...
- ant svn
<!-- 检出代码 这里使用 export 不是checkout 二者区别 checkout会svn相关信息文件检出,export只是检出最新的文件--> <target name= ...
- Docker 01 Introduction
Docker的组成: Docker Engine,一个轻量级.强大的开源容器虚拟化平台,使用包含了工作流的虚拟化技术,帮助用户建立.并容器化一个应用. Docker Hub,提供的一个SaaS服务,用 ...
- IOS UI 第五篇:基本UI
添加个导航栏: Xib1 *xib1 = [[Xib1 alloc] initWithNibName:@"Xib1" bundle:nil]; UINavig ...
- iOS基础 - UIScrollView
一.UIScrollView使用引导思路. 1.之前我们所学的显示图片是用UIImageView 2.将UIImageView添加到根视图中,不显示的原因:没有设置位置 3.当图片比屏幕大时,直接放在 ...
- Asp.Net Web Api 与 Andriod 接口对接开发
Asp.Net Web Api 与 Andriod 接口对接开发经验,给小伙伴分享一下! 最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用A ...
- c++类的构造函数与析构函数
为什么用构造函数与析构函数 构造函数: c++目标是让使用类对象就像使用标准类型一样,但是常规化的初始化句法不适用与类类型. ; //基本类型 struct thing { char *pn; int ...
- Definition of:payload
(1) Refers to the "actual data" in a packet or file minus all headers attached for transpo ...