【源码】进入ASP.NET MVC流程的大门 - UrlRoutingModule
UrlRoutingModule的功能
在ASP.NET MVC的请求过程中,UrlRoutingModule的作用是拦截当前的请求URL,通过URL来解析出RouteData,为后续的一系列流程提供所需的数据,比如Controller、Action等等。其实,UrlRoutingModule和我们自定义的HttpModule都是实现了IHttpModule接口的两个核心方法,Init方法和Dispose方法。下面是MVC中实现UrlRoutingModule代码。首先,在初始化的方法中检查该Module是否被加入到了当前请求的请求管道,然后注册了管道事件中的PostResolveRequestCache事件。其实最理想的注册事件应该是MapRequestHandler事件,但是为了考虑到兼容性(IIS 6 和 IIS 7 ISAPI模式下不可用),微软选择了紧邻MapRequestHandler事件之前的PostResolveRequestCache事件。
protected virtual void Init(HttpApplication application)
{
// 检查 UrlRoutingModule 是否已经被加入到请求管道中
if (application.Context.Items[_contextKey] != null)
{
// 已经添加到请求管道则直接返回
return;
}
application.Context.Items[_contextKey] = _contextKey; // 理想情况下, 我们应该注册 MapRequestHandler 事件。但是,MapRequestHandler事件在
// II6 或 IIS7 ISAPI 模式下是不可用的,所以我们注册在 MapRequestHandler 之前执行的事件,
// 该事件适用于所有的IIS版本。
application.PostResolveRequestCache += OnApplicationPostResolveRequestCache;
}
在注册事件中,将HttpApplication的请求上下文HttpContext做了一个封装,因为HttpContext是没有基类的,也不是Virtual的,所以没办法做单元测试,也就是不可Mock的,所以针对HttpContext做了一个封装。HttpContextBase是HttpContextWrapper的基类,真正封装HttpContext的就是HttpContextWrapper,所以三者之间的关系就是这样的。完成封装以后开始执行PostResolveRequestCache方法,并将封装好的请求上下文作为参数传入。
private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
{
//HttpContextWrapper继承自HttpContextBase,用于替换HttpContext以实现可测试的接口
HttpApplication app = (HttpApplication)sender;
HttpContextBase context = new HttpContextWrapper(app.Context);
PostResolveRequestCache(context);
}
进入PostResolveRequestCache事件后,UrlRoutingModule开始真正的工作,该方法是处理URL的核心方法。根据当前请求的上下文,去匹配路由表是否存在与之匹配的URL,如果匹配则从路由信息中获取RouteData,以及IRouteHandler。拿到IRouteHandler后,要进行一些列的判断,比如要判断是否是StopRoutingHandler,在Global.asax文件中有一段RouteConfig.RegisterRoutes(RouteTable.Routes);代码,在这个RegisterRoutes方法中有一句routes.IgnoreRoute("{resource}.axd/{*pathInfo}");表示需要过滤掉的路由,而这个IgnoreRoute路由的Handler就是StopRoutingHandler,所以在这里做了判断,Handler是StopRoutingHandler则不往下执行,直接return,不再处理这条请求,如果是则路由模块会停止继续处理请求,如果不是则继续处理,并根据RequestContext来获取IHttpHandler,拿到IHttpHandler后还要继续验证是不是UrlAuthFailureHandler,UrlAuthFailureHandler也实现了IHttpHandler,当当前请求无权限时,用于返回401错误。
public virtual void PostResolveRequestCache(HttpContextBase context)
{
// 匹配传入的URL,检查路由表中是否存在与之匹配的URL
RouteData routeData = RouteCollection.GetRouteData(context); // 如果没有找到匹配的路由信息,直接返回
if (routeData == null)
{
return;
} // 如果找到的匹配的路由,则从路由信息的RouteHandler中获取IHttpHandler
IRouteHandler routeHandler = routeData.RouteHandler;
if (routeHandler == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.UrlRoutingModule_NoRouteHandler)));
} // 如果该IRouteHandler是StopRoutingHandler,路由模块会停止继续处理该请求
// routes and to let the fallback handler handle the request.
if (routeHandler is StopRoutingHandler)
{
return;
} RequestContext requestContext = new RequestContext(context, routeData); // 将路由信息添加到请求上下文
context.Request.RequestContext = requestContext; IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
if (httpHandler == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, SR.GetString(SR.UrlRoutingModule_NoHttpHandler), routeHandler.GetType()));
} // 如果该IHttpHandler是认证失败的IHttpHandler,返回401权限不足错误
if (httpHandler is UrlAuthFailureHandler)
{
if (FormsAuthenticationModule.FormsAuthRequired)
{
UrlAuthorizationModule.ReportUrlAuthorizationFailure(HttpContext.Current, this);
return;
}
else
{
throw new HttpException(, SR.GetString(SR.Assess_Denied_Description3));
}
} // Remap IIS7 to our handler
context.RemapHandler(httpHandler);
}
如果请求认证失败,返回401错误,并且调用CompleteRequest方法,显式地完成当前请求。
internal static void ReportUrlAuthorizationFailure(HttpContext context, object webEventSource)
{
// 拒绝访问
context.Response.StatusCode = ;
WriteErrorMessage(context); if (context.User != null && context.User.Identity.IsAuthenticated) {
// 这里AuditUrlAuthorizationFailure指示在Web请求过程中URL授权失败的事件代码
WebBaseEvent.RaiseSystemEvent(webEventSource, WebEventCodes.AuditUrlAuthorizationFailure);
}
context.ApplicationInstance.CompleteRequest();
}
方法GetRouteData的作用是根据当前请求的上下文来获取路由数据,在匹配RouteCollection集合之前,会检查当前的请求是否是静态文件,如果请求的是存在于服务器上的静态文件则直接返回,否则继续处理当前请求。
public RouteData GetRouteData(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
if (httpContext.Request == null)
{
throw new ArgumentException(SR.GetString(SR.RouteTable_ContextMissingRequest), "httpContext");
} // Optimize performance when the route collection is empty.当路由集合是空的的时候优化性能. The main improvement is that we avoid taking
// a read lock when the collection is empty.主要的改进是当集合为空的时候避免添加只读锁。 Without this check, the UrlRoutingModule causes a 25%-50%
// 没有这个检查的话,UrlRoutingModule 性能会因为锁的缘故而下降25%-50%
// regression in HelloWorld RPS due to lock contention. The UrlRoutingModule is now in the root web.config,
// UrlRoutingModule目前被配置在根目录的web.config
// so we need to ensure the module is performant, especially when you are not using routing.
// 所以我们应该确认下这个module是否是高效的,尤其是当没有使用路由的时候。
// This check does introduce a slight bug, in that if a writer clears the collection as part of a write
// transaction, a reader may see the collection when it's empty, which the read lock is supposed to prevent.
// We will investigate a better fix in Dev10 Beta2. The Beta1 bug is Dev10 652986.
if (Count == ) {
return null;
} bool isRouteToExistingFile = false;
// 这里只检查一次
bool doneRouteCheck = false;
if (!RouteExistingFiles)
{
isRouteToExistingFile = IsRouteToExistingFile(httpContext);
doneRouteCheck = true;
if (isRouteToExistingFile)
{
// If we're not routing existing files and the file exists, we stop processing routes
// 如果文件存在,但是路由并没有匹配上,则停止继续处理当前请求。
return null;
}
} // Go through all the configured routes and find the first one that returns a match
// 遍历所有已配置的路由并且返回第一个与之匹配的
using (GetReadLock())
{
foreach (RouteBase route in this)
{
RouteData routeData = route.GetRouteData(httpContext);
if (routeData != null)
{
// If we're not routing existing files on this route and the file exists, we also stop processing routes
if (!route.RouteExistingFiles)
{
if (!doneRouteCheck)
{
isRouteToExistingFile = IsRouteToExistingFile(httpContext);
doneRouteCheck = true;
}
if (isRouteToExistingFile)
{
return null;
}
}
return routeData;
}
}
}
return null;
}
下面这段代码就是获取相对路径来检测文件夹和文件是否存在,存在返回true,否则返回false。
// 如果当前请求的是一个存在的文件,则返回true
private bool IsRouteToExistingFile(HttpContextBase httpContext)
{
string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath;
return ((requestPath != "~/") &&
(VPP != null) &&
(VPP.FileExists(requestPath) ||
VPP.DirectoryExists(requestPath)));
}
如果文中有表述不正确或有疑问的可以在评论中指出,一起学习一起进步!!
【源码】进入ASP.NET MVC流程的大门 - UrlRoutingModule的更多相关文章
- 通过源码了解ASP.NET MVC 几种Filter的执行过程 在Winform中菜单动态添加“最近使用文件”
通过源码了解ASP.NET MVC 几种Filter的执行过程 一.前言 之前也阅读过MVC的源码,并了解过各个模块的运行原理和执行过程,但都没有形成文章(所以也忘得特别快),总感觉分析源码是大神 ...
- Solr4.8.0源码分析(5)之查询流程分析总述
Solr4.8.0源码分析(5)之查询流程分析总述 前面已经写到,solr查询是通过http发送命令,solr servlet接受并进行处理.所以solr的查询流程从SolrDispatchsFilt ...
- EDKII Build Process:EDKII项目源码的配置、编译流程[三]
<EDKII Build Process:EDKII项目源码的配置.编译流程[3]>博文目录: 3. EDKII Build Process(EDKII项目源码的配置.编译流程) -> ...
- 通过官方API结合源码,如何分析程序流程
通过官方API结合源码,如何分析程序流程通过官方API找到我们关注的API的某个方法,然后把整个流程执行起来,然后在idea中,把我们关注的方法打上断点,然后通过Step Out,从内向外一层一层分析 ...
- Cocos2dx源码赏析(1)之启动流程与主循环
Cocos2dx源码赏析(1)之启动流程与主循环 我们知道Cocos2dx是一款开源的跨平台游戏引擎,而学习开源项目一个较实用的办法就是读源码.所谓,"源码之前,了无秘密".而笔者 ...
- RxJava && Agera 从源码简要分析基本调用流程(2)
版权声明:本文由晋中望原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/124 来源:腾云阁 https://www.qclo ...
- (转)linux内存源码分析 - 内存回收(整体流程)
http://www.cnblogs.com/tolimit/p/5435068.html------------linux内存源码分析 - 内存回收(整体流程) 概述 当linux系统内存压力就大时 ...
- HDFS源码分析DataXceiver之整体流程
在<HDFS源码分析之DataXceiverServer>一文中,我们了解到在DataNode中,有一个后台工作的线程DataXceiverServer.它被用于接收来自客户端或其他数据节 ...
- MyBatis 源码篇-SQL 执行的流程
本章通过一个简单的例子,来了解 MyBatis 执行一条 SQL 语句的大致过程是怎样的. 案例代码如下所示: public class MybatisTest { @Test public void ...
随机推荐
- 使用SQL Server Management Studio操作replication时,要用机器名登录,不要用IP地址
如果你在使用SSMS(SQL Server Management Studio)登录SQL Server时,使用的是IP地址,如下图所示: 当你操作replication时,会报错: 从上面的错误提示 ...
- MySQL MTS复制: hitting slave_pending_jobs_size_max
测试步骤: 从库停止复制:stop slave; 主库创建大表400万条记录. 开启从库复制:start slave; 监测从库error log持续输出: 2018-12-06T10:40:52.6 ...
- 35_张孝祥Java高新技术_为注解增加各种属性
注解的作用之所以那么强大,就是因为它有属性 注解很像接口,属性很像方法. 什么是注解的属性 一个注解相当于一个胸牌,如果你胸前贴了胸牌,就是传智播客的学生,否则,就不是.如果还想区分出事传智播客 ...
- CreateEvent
事件对象就像一个开关:它只有两种状态---开和关.当一个事件处于”开”状态,我们称其为”有信号”否则称为”无信号”.可以在一个线程的执行函数中创建一个事件对象,然后观察它的状态,如果是”无信号”就让该 ...
- Beta版本项目计划
小队名称:PHILOSOPHER 小组成员 [组长]金盛昌(201421122043).刘文钊(20142112255).陈笑林(201421122042) 张俊逸(201421122044).陈志建 ...
- 前端工程构建工具之Yeoman
一.Yeoman 简介 通常在开发新项目时我们都需要配置工程环境,开发目录,需要下载一些库.框架文件(如 jQuery.Backbone 等),配置编译环境(Less.Sass.Coffeescrip ...
- Universal-Image-Loader源码分析(一)——ImageLoaderConfiguration分析
UIl与Volley一样是非常古老的框架,UIL实现了从网络获取图片,对图片进行缓存,以及根据个性化的设置来将图片加载到ImageView上. 这篇文章 主要分析UIl在初始化配置的源码 UIL初始化 ...
- elk平台定制化查询规则
一.查询某IP在某时间内TOP10的请求 步骤: 点击“Visualize”选项卡 创建“Data table” 点击“From a new search” 下拉选择“F5-access” 在“buc ...
- Mac svn使用学习-1-简介
在Windows环境中,可以使用TortoiseSVN来搭建svn环境.但是由于Mac自带了svn的服务器端和客户端功能,因此可以直接使用svn功能. svn即subversion,Subversio ...
- Jmeter之目录结构
首先得了解一下这些东西,以后才能快速的找到某些配置文件进行修改(举个例子,改配置只是其中之一) 一.bin目录examples: 目录中有CSV样例 jmeter.bat windows的启动文件 ...