【源码】进入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 ...
随机推荐
- linux 设备驱动加载的先后顺序
Linux驱动先注册总线,总线上可以先挂device,也可以先挂driver,那么究竟怎么控制先后的顺序呢. 1.初始化宏 Linux系统使用两种方式去加载系统中的模块:动态和静态. 静态加载:将所有 ...
- python的学习之路day4
大纲 1.一些常用的内置函数 callable() chr() & ord() 随机生成验证码 map() 全局变量,局部变量 hash() & round() max() min() ...
- Hadoop HBase概念学习系列之HLog(二)
首先,明确,HRegion服务器包含两大部分:HLog和HRegion. HLog用来存储数据日志,采用的是先写日志的方式. 当用户需要更新数据的时候,数据会被分配到对应的HRegion服务器上提交修 ...
- lavarel模板引擎blade学习
blade 模板学习 特点 主要的两个优点是:模板继承和区块 继承页面布局 布局文件(layout.php) + 详情文件 (page.php) 的组合,即一般到具体的组合.在blade文件之中的体现 ...
- 《python源代码剖析》笔记 python环境初始化
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zhsenl/article/details/33747209 本文为senlie原创.转载请保留此地 ...
- 【洛谷】【最小生成树】P1195 口袋的天空
[题目背景:] 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空. 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖. [题目描述:] 给你云朵的个数N,再给你M个关系,表示哪些 ...
- mysql 批量修改字段方法
一.正式环境操作注意事项: .关闭应用访问或者设置数据库只读 mysql设为只读方法: 开启只读: mysql> show global variables like "%read_o ...
- UML类图简单学习 各种对象、关系UML表示法
<大话设计模式>上面的UML类图: 类的UML表示 动物 的矩形框 表示是一个类. 类图分为三层,第一层显示类的名称,如果是抽象类,则用斜体表示:第二层是类的特性,通常就是类的字段和属性: ...
- Python2.7-os.path
os.path 模块,实现了对文件路径的操作,但是不操作文件.由于不同系统文件路径格式不同,os.path 总是调用适合当前系统的版本,你也可以手动导入别的系统的(posixpath,ntpath,m ...
- 四,ESP8266 TCP服务器(基于Lua脚本语言)
我要赶时间赶紧写完所有的内容....朋友的东西答应的还没做完呢!!!!!!!没想到又来了新的事情,,....... 配置模块作为TCP服务器然后呢咱们连接服务器发指令控制LED亮灭 控制的指令呢咱就配 ...