UrlRoutingModule的功能

在ASP.NET MVC的请求过程中,UrlRoutingModule的作用是拦截当前的请求URL,通过URL来解析出RouteData,为后续的一系列流程提供所需的数据,比如ControllerAction等等。其实,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做了一个封装。HttpContextBaseHttpContextWrapper的基类,真正封装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后还要继续验证是不是UrlAuthFailureHandlerUrlAuthFailureHandler也实现了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的更多相关文章

  1. 通过源码了解ASP.NET MVC 几种Filter的执行过程 在Winform中菜单动态添加“最近使用文件”

    通过源码了解ASP.NET MVC 几种Filter的执行过程   一.前言 之前也阅读过MVC的源码,并了解过各个模块的运行原理和执行过程,但都没有形成文章(所以也忘得特别快),总感觉分析源码是大神 ...

  2. Solr4.8.0源码分析(5)之查询流程分析总述

    Solr4.8.0源码分析(5)之查询流程分析总述 前面已经写到,solr查询是通过http发送命令,solr servlet接受并进行处理.所以solr的查询流程从SolrDispatchsFilt ...

  3. EDKII Build Process:EDKII项目源码的配置、编译流程[三]

    <EDKII Build Process:EDKII项目源码的配置.编译流程[3]>博文目录: 3. EDKII Build Process(EDKII项目源码的配置.编译流程) -> ...

  4. 通过官方API结合源码,如何分析程序流程

    通过官方API结合源码,如何分析程序流程通过官方API找到我们关注的API的某个方法,然后把整个流程执行起来,然后在idea中,把我们关注的方法打上断点,然后通过Step Out,从内向外一层一层分析 ...

  5. Cocos2dx源码赏析(1)之启动流程与主循环

    Cocos2dx源码赏析(1)之启动流程与主循环 我们知道Cocos2dx是一款开源的跨平台游戏引擎,而学习开源项目一个较实用的办法就是读源码.所谓,"源码之前,了无秘密".而笔者 ...

  6. RxJava && Agera 从源码简要分析基本调用流程(2)

    版权声明:本文由晋中望原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/124 来源:腾云阁 https://www.qclo ...

  7. (转)linux内存源码分析 - 内存回收(整体流程)

    http://www.cnblogs.com/tolimit/p/5435068.html------------linux内存源码分析 - 内存回收(整体流程) 概述 当linux系统内存压力就大时 ...

  8. HDFS源码分析DataXceiver之整体流程

    在<HDFS源码分析之DataXceiverServer>一文中,我们了解到在DataNode中,有一个后台工作的线程DataXceiverServer.它被用于接收来自客户端或其他数据节 ...

  9. MyBatis 源码篇-SQL 执行的流程

    本章通过一个简单的例子,来了解 MyBatis 执行一条 SQL 语句的大致过程是怎样的. 案例代码如下所示: public class MybatisTest { @Test public void ...

随机推荐

  1. 检索 COM 类工厂中 CLSID 为 {00021A20-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80080005

    创建Excel对象失败: Excel.Application xApp = new Excel.Application(); 错误提示:{"检索 COM 类工厂中 CLSID 为 {0002 ...

  2. "==" equals和hashCode的联系和区别

    写这篇文章的目的是自己彻底把三者搞清楚,也希望对你有所帮助. 1."=="运算符对与基本类型(int long float double boolean byte char sho ...

  3. jQuery插件实例七:一棵Tree的生成史

    在需要表示级联.层级的关系中,Tree作为最直观的表达方式常出现在组织架构.权限选择等层级关系中.典型的表现形试类似于: 一颗树的生成常常包括三个部分:1)数据库设计:2)后台程序:3)前端代码.那么 ...

  4. npm WARN unmet dependency问题的解决方法

    remove node_modules $ rm -rf node_modules/ run $ npm cache clean 详见这里: http://stackoverflow.com/ques ...

  5. python第三十课--异常(异常处理定义格式和常见类型)

    演示: 1).异常处理的定义格式: 2).常见的运行时异常类型: try: print(10/0) num=int('132a') except Exception as e: print('出错了. ...

  6. Promise 模式解析:Promise模式与异步及声明式编程

    一.构建流程 1.(异步)数据源(请求)的构建:Promise的构建并执行请求: 2.处理流程的构建:then将处理函数保存: 二.处理: 1.请求的响应返回: 2.调用后继处理流程. 三. 1.构建 ...

  7. 关于 Spring AOP (AspectJ) 该知晓的一切

    关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...

  8. nginx下No input file specified错误的解决

    在web服务的根目录下创建 .htaccess文件,设置一下内容: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond % ...

  9. bat 传递超过10个参数(bat参数遍历)

    批处理文件中可引用的参数为%0~%9, %0是指批处理文件的本身,也可以说是一个外部命令:%1~%9是批处理参数,也称形参:而替换形参的实参若超过了批处理文件中所规定数值(9个)且想在批处理文件中应用 ...

  10. ASP.NET Core的Kestrel服务器(转载)

    Kestrel是一个基于libuv的跨平台ASP.NET Core web服务器,libuv是一个跨平台的异步I/O库.ASP.NET Core模板项目使用Kestrel作为默认的web服务器.Kes ...